How to get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, i will explain that.

normally in Laravel, we have to use controllers and models to get data from a database to a view. today I will show you how to call a model two all pages of a Laravel application.

For that, we have to modify the AppServiceProvider.php file. we have to add some codes inside the boot() function.

Step 1: Go to app/Providers/AppServiceProvider.php

app/Providers/AppServiceProvider.php 

Step 2: Add View Share function

use App\Models\setting;   //call your model
 
class AppServiceProvider extends ServiceProvider 
{
    public function boot() 
    {
        $setings = setting::find(1);                   
        View::share(['settings' =>$setings]);   
    }
}

Step 3: Call the data in Blade template

{{ $settings->websitename }}

Thats all . this is how its works ..  i will give you another example how you can call a single variable to all views

 

How to call a variable in all views in Laravel

class AppServiceProvider extends ServiceProvider 
{
    public function boot() 
    {
        view()->share('sitename', 'My Awesome Website');
    }
}

Same like in blade template you can call this anywhere

{{$sitename}}

Simple as that. Hope this helps

Check out other awesome Laravel tutorials 



 

 

 

 

 

 

 

 

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Laravel Eloquent WhereNull() and whereNotNull() Example

Do you know how to use WhereNull() and whereNotNull() Laravel Eloquent. In this post, I will explain how to use […]

July 28, 2022

Laravel Array to JSON Response From Laravel Controller

Do you know how to make JSON responses from a Laravel array? As a developer someday we all have to […]

July 28, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now