baguus
232
2
Laravel

Passing custom query results to layout template part

Posted 1 year ago by baguus

Hi I'm a bit confused on how to do this. I have a template part (budget.blade.php) that I'm including in my template (@include). I need to get variables from a custom query based on user into this template part . (users company budget, orders total,...). I tried with a Service provider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\View\View;
use App\Order;
use App\Product;
use App\Company;
use Auth;

class BudgetServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('layouts.budget', function () {
            // Can I perform my queries here and if how do I pass them to layouts/budget.blade.php?
        });

    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Partial view

<div class="budget-info-wrapper">
    <div class="budget">
        Forseen budget: {{ $var1}}
    Order total: {{ $var2}}
    Difference: {{ $var3}}      
    </div>
</div>

Is this the correct way and If so, how do I pass the variables to my template part?

Please sign in or create an account to participate in this conversation.