0

I'm new on Laravel and Blade. I've two different contents, one for Home, and second for any view, excluding home.

I've this code but nothing happens:

@if(view('home')) // Display content if is home, display 'foo'
    @include('partials/foo')
@else
    @include('partials/bar') // Okay, not in home, display 'bar'
@endif;

Is this the correct way?

Carlos Roman
  • 81
  • 1
  • 9

3 Answers3

0

Use Route class in blade to get the method and decide based on method

@if(\Route::getCurrentRoute()->getActionMethod()  == 'index')
    @include('partials/foo')
@else
   @include('partials/bar') // Okay, not in home, display 'bar'
@endif;
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51
0

I solved using this How to get the current URL inside @if statement (blade) in Laravel 4?

@if(Request::is('/')) // Homepage
    'foo'
@else // All pages
    'bar'
@endif
Carlos Roman
  • 81
  • 1
  • 9
0

Typically, you would use a controller. The flow is as follows:

  • your routes.web file has a route, such as /home
  • this points to a public method on a controller, such as HomeController@index()
  • The controller does any queries and calculations and then returns a view with needed data

In routes.web:

Route::get('home', 'HomeController@index');

In HomeController class:

public function index()
{
    $variable = ModelName::where('field', 'value')->first();

    return view('home')->with('variable', $variable);
}

This allows you to not have to check the view in blade because you are specifically sending it there.

Remove the variable stuff if you don't need it.

parker_codes
  • 3,267
  • 1
  • 19
  • 27