develpr
develpr

Reputation: 1406

How do I use a controller for a "partial" view in Laravel?

Here is my situation. I have a layout.blade.php which most of my pages use. Within this file, I have some partial pieces that I include, like @include('partials.header'). I am trying to use a controller to send data to my header.blade.php file, but I'm confused as to exactly how this will work since it is included in every view that extends layout.blade.php.

What I am trying to do is retrieve a record in my database of any Game that has a date of today's date, if it exists, and display the details using blade within the header.

How can I make this work?

Upvotes: 5

Views: 5094

Answers (4)

Nico
Nico

Reputation: 121

The better solution would be this

Under your app folder make a class named yourClassNameFacade. Your class would look like this.

class yourClassNameFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'keyNameYouDecide';
    }
}

Then go to the file app/Providers/AppServiceProvider.php and add to the register function

public function register()
    {
        $this->app->bind('keyNameYouDecide', function (){
            //below your logic, in my case a call to the eloquent database model to retrieve all items.
            //but you can return whatever you want and its available in your whole application.
            return \App\MyEloquentClassName::all();
        });
    }

Then in your view or any other place you want it in your application you do this to reference it.

view is the following code:

{{ resolve('keyNameYouDecide') }}

if you want to check what is in it do this:

{{ ddd(resolve('keyNameYouDecide')) }}

anywhere else in your code you can just do:

resolve('keyNameYouDecide'))

Upvotes: 0

Richard F
Richard F

Reputation: 259

In Laravel you can create a service class method that acts like a controller and use @inject directive to access this in your partial view. This means you do not need to create global variables in boot(), or pass variables into every controller, or pass through the base view layout.blade.php.

resources/views/header.blade.php:
@inject('gamesToday', 'App\Services\GamesTodayService')
@foreach ($gamesToday->getTodayGames() as $game)
  // display game details
@endforeach

Upvotes: 1

MohamedSabil83
MohamedSabil83

Reputation: 1559

While it's different value you retrieved belong of the game chosen, you can do something like that:

Controller

$data = Game::select('id', 'name', 'published_date')->first();
return view('game')->with(compact('data'));

layout.blade.php

<html><head></head><body>
{{ $date }}
</body></html>

game.blade.php

@extend('layout')
@section('date', $data->date)
@section('content')

@endsection

Upvotes: 0

Chay22
Chay22

Reputation: 2894

I think to define those Game as globally shared is way to go.

In your AppServiceProvider boot method

public function boot()
{

    view()->composer('partials.header', function ($view) {
        view()->share('todayGames', \App\Game::whereDay('created_at', date('d')->get());
    });

    // or event view()->composer('*', Closure) to share $todayGames accross whole blade
}

Render your blade as usual, partial.header blade

@foreach ($todayGames as $game)
  // dostuffs
@endforeach

Upvotes: 4

Related Questions