
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
- Get count of filter data, while return a small set of records
- The use statement with non-compound name 'Auth' has no effect
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Delete all related comments on deleting a post in Laravel
- How to get IP address in laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Laravel csrf token mismatch for ajax POST Request
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Ajax POST request in laravel
- Convert multidimensional array to single array in Laravel
- Laravel route redirect not working
- Get only 10 records from table in laravel
- Composer\Exception\NoSslException
- How to get path from current URL in Laravel
- Extract only time from datetime in laravel
- Php artisan make model, factory, migration and controller in single command
- JQuery each loop on json response after ajax in laravel
- Always load the relationship data with eager loading in Laravel
- Laravel URL validation not working
- How to create new user without form submission in laravel
- How to insert dynamic value to additional column in pivot table in laravel
- InRandomOrder() method with example in laravel
- How to call model in blade laravel
- How to validate form input data in laravel
- Wheredate in laravel not working