
How to pass data to partial view file in laravel
How to pass data to partial view file in laravel
In this article, we will see how to solve "How to pass data to partial view file in laravel".
Partial view file is a common file which is used to include in all others layout and individual files in laravel. So here we have the problem of passing the data to partials file by a controller method because we can’t pass data to partials by calling the same code again and again in the controller's method. Which is not a good practice so here we use the AppServiceProvider to do so.-
Passing data to partials/common file using AppServiceProvider in laravel
//app\Providers\AppServiceProvider.php public function boot() { \View::composer('partials/header', function ($view) { $userDetails= DB::table('users')->select('id','name','email')->first(); $view->with(['user'=>$userDetails]); }); //resources\views\partials\header.blade.php {{ $user->name }}
You can pass data to the partial file using appserviceprovider and can display it in the partial view file. Now you can include the partial file in your layout and individual file.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to add a key value pair to existing array in laravel
- How to call Laravel route in jQuery
- Get current month records in laravel 7/8
- How to create belongstomany relation using custom name on custom pivot table
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to get random string in Laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Add class to body in laravel view
- How to display a specific word from a string in laravel
- Remove public from url in laravel project
- Array to string conversion laravel Controller
- How to get IP address in laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- Insert current date time in a column using Laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- How to display order by null last in laravel
- Undefined property: stdClass::$title
- How to avoid duplicate entries in pivot table in Laravel
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- How to check email is valid or not in Laravel
- Input file with max size validation in laravel
- How to check records exist in loaded relationship in Laravel blade view
- Laravel get single row by id
- Import/Use Storage facade in laravel