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 integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

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

Get Free Access Now