John Davidson

php - Global variable in laravel not working after upgrading 7 to 8

0 comments
Message:


I upgraded the laravel from 7 to 8 and it works very well except for the global variables.
this is my sample:


View::composer('*', function ($view) {
$contact = App\Contact::all();
$topbar = App\Topbar::orderBy('sortorder', 'asc')->get();

$view->with('contact',$contact)
->with('topbar',$topbar);

});

you can find this code in official docs:
https://laravel.com/docs/8.x/views



My Answer:
>get();_ view()->share('contact', $contact);_ view()->share('topbar', $topbar);_});_In Laravel 8, global variables are no longer supported in the same way as in previous versions. Instead, you can use view composers or view creators to share data with all views.

Here's how you can update your code to work in Laravel 8:

php
use Illuminate\Support\Facades\View;

View::composer('*', function ($view) {
$contact = App\Contact::all();
$topbar = App\Topbar::orderBy('sortorder', 'asc')->get();

$view->with('contact', $contact);
$view->with('topbar', $topbar);
});


By using the `with` method on the `$view` object, you can pass data to all views in Laravel 8. This should work the same way as global variables did in Laravel 7.

Rate this post

3 of 5 based on 3422 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us