Latest

Monday, July 10, 2017

laravel pass parameter to master layout before render?

Asked by: Hanik


i am making an application and i want that pass error parameters to master view use within partial blade called error.blade.php.

i am checking some variable in controller and if anything occur error i added it to error array as below :

$error[] = 'error 1';

and i want to pass this to partial blade view called error.blade.php and render it but how can i do this ?

this array could be empty or not.

how can i pass this array to master layout before render ?

i use this error array every controller.

please help me


Answers

Answered by: Yat23 at 2017-07-10 07:58PM



You can use a View Composer and just pass the data in within the boot() method of the provider.

    View::composer('master', function ($view) {
       $view->with('errors', $errors);          
    });

The $errors will be available in your view now.



Answered by: Maraboc at 2017-07-10 08:57PM



All you need to do is add this line :

\Illuminate\View\Middleware\ShareErrorsFromSession::class,

To your app/Http/Kernel.php especially in the protected $middleware = array

And all you need to do is add ->withErrors($errors) to redirect or view methods and $errors variable will be accessible everywhere in the view even if the master layout




Source

No comments:

Post a Comment

Adbox