Monday, April 16, 2018

save collection in global variable in laravel 5.6 - development

save collection in global variable in laravel 5.6

so basically i have an index page where i have to go to controller to fetch data from database

route:-

route::get('/index/{$x}', 'indexController@index');

index controller:-

$data = Data::where('name', $x)->get();
return View::make('index', compact('data');

then inside index page i have link that goes to second page
with same data, i d not want to query the same data as it may affect performance

route:-

route::get('/second', indexController@second);});

Second Controller:-

$data = $data->sortBy('id');
return View::make('second', compact('data');

i thought of saving data in global variable in controller

so i added private variable inside controller and try to access it through $this->data

but it did not work cause based on my search the controller will be closed after it returns view

so if i try access $this->data inside second function it will be empty

is it possible to save queried data (collection) in global variable

as i do not want to query same data for every page

your help will be appreciated



from Laravel Questions and Answers https://laravelquestions.com/laravel/save-collection-in-global-variable-in-laravel-5-6/
via Lzo Media

No comments:

Post a Comment