I
I
it_pear_yurec2021-07-10 17:26:04
Laravel
it_pear_yurec, 2021-07-10 17:26:04

How to pass a variable to the master template?

I can’t find an answer anywhere, how to pass a variable to the mater template, I have a shopping cart, I make a variable to display products, on the product page, I display this variable, since the controller calls the model and searches for a variable, but how to pass this variable in the master template and so that all this is displayed on any page, and does not give an error about a variable not found? I try through AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use App\Models\Order;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {   
        $orderId = session('orderId');
        $order = Order::find($orderId);   
        View::share('order', $order);
    }
}

p.s. if you pass just a string, then everything is good, but otherwise null.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question