122

In Laravel 4, my controller uses a Blade layout:

class PagesController extends BaseController {
    protected $layout = 'layouts.master';
}

The master layout has outputs the variable title and then displays a view:

...
<title>{{ $title }}</title>
...
@yield('content')
....

However, in my controller I only appear to be able to pass variables to the subview, not the layout. For example, an action could be:

public function index()
{
    $this->layout->content = View::make('pages/index', array('title' => 'Home page'));
}

This will only pass the $title variable to the content section of the view. How can I provide that variable to the whole view, or at the very least the master layout?

13 Answers 13

269

If you're using @extends in your content layout you can use this:

@extends('master', ['title' => $title])

Note that same as above works with children, like:

@include('views.subView', ['my_variable' => 'my-value'])

Usage

Then where variable is passed to, use it like:

<title>{{ $title ?? 'Default Title' }}</title>
Sign up to request clarification or add additional context in comments.

3 Comments

It took me a lot of googling to find this! It is just this syntax that you got to get perfect and docs don't show all these subtleties. I mean this is what official docs say @component('alert', ['foo' => 'bar'])......
I am doing this and I am getting an undefined error on the layout page. Any ideas?
@LucyTurtle see usage section (provide default value, like: {{ $title ?? 'My Default Title' }})
53

For future Google'rs that use Laravel 5, you can now also use it with includes,

@include('views.otherView', ['variable' => 1])

2 Comments

this is what im looking for to finally create a component based application on laravel, thanks!
if I only pass ["one"], how do I get it in the template?
25

In the Blade Template : define a variable like this

@extends('app',['title' => 'Your Title Goes Here'])
@section('content')

And in the app.blade.php or any other of your choice ( I'm just following default Laravel 5 setup )

<title>{{ $title or 'Default title Information if not set explicitly' }}</title>

This is my first answer here. Hope it works.Good luck!

2 Comments

This helped me save few hours..! Used it in Laravel 5.4 and it works great.. Thanks for the answer...
I had to do: {{ $title ?? 'Default Title' }}
21

I was able to solve that problem by adding this to my controller method:

    $title = 'My Title Here';
    View::share('title', $title);

$this->layout->title = 'Home page'; did not work either.

1 Comment

This worked for me in laravel 5.1. Easiest way to solve the problem.
7

Simplest way to solve:

view()->share('title', 'My Title Here');

Or using view Facade:

use View;

...

View::share('title', 'My Title Here');

1 Comment

can i execute it in a route middleware?
4

It appears as though I can pass variables to the entire layout using attributes on the layout object, for example to solve my problem I was able to do the following:

$this->layout->title = 'Home page';

2 Comments

This didn't work for me. I currently get an error stating that it does not exist as a variable.
where can I find the layout object?
3

Following is simple solution worked for me.

In layout

    <title>@yield('Page-Title') </title>

In your blade file 

@section('Page-Title')
My Page Title
@endsection 

1 Comment

It works with me on laravel 8 based project, thank you very much
1
class PagesController extends BaseController {
    protected $layout = 'layouts.master';

    public function index()
    {
        $this->layout->title = "Home page";
        $this->layout->content = View::make('pages/index');
    }
}

At the Blade Template file, REMEMBER to use @ in front the variable.

...
<title>{{ $title or '' }}</title>
...
@yield('content')
...

1 Comment

@YevgeniyAfanasyev, this question you should ask back post owner. I just refer back his coding. Kind of weird you ask back me.... And this was answered in 2014. Of course I was referring on the version he asking which is Laravel 4.
1

just try this simple method: in controller:-

 public function index()
   {
        $data = array(
            'title' => 'Home',
            'otherData' => 'Data Here'
        );
        return view('front.landing')->with($data);
   }

And in you layout (app.blade.php) :

<title>{{ $title }} - {{ config('app.name') }} </title>

Thats all.

2 Comments

Could you explain how this solves the problem? And what new does it contribute over and above the previous (especially accepted) answers?
Welcome to SO! When you place an answer, even if it is ok, you should explain it a little bit, and in your case, as there are one similar, try to explain the pros and cons of your answer.
1

if you want to get the variables of sections you can pay like this:

$_view      = new \View;
$_sections  = $_view->getFacadeRoot()->getSections();
dd($_sections);
/*
Out:
array:1 [▼
  "title" => "Painel"
]
*/

Comments

0

I have my layout file defined as a component. I configured the component to receive a variable as an argument. I have my variable defined in my controller as the full DB CMS Model passed in a return view to the blade file. From there, I simply pass the variable when calling the component from within the blade file. Please note that doing it this way requires the long form using ":". I can't get it to recognize the variable inside the component call using the short form.

My controller:

    public function show($id)
    {
        $document = Document::Post()->findOrFail($id);

        return view('pages.blog.post.show-post')
            ->with("document", $document);
    }

My component call within the view "show-post"

<x-layout.layout-master
    :document=$document   
>

Inside the master, the $document variable is now directly available and I can access anything from the Model.

The primary reason that I needed to do this is that I store default layout variables defined as attributes in the Model based on the document type, every html page starts from a document record. Sometimes I need to override those defaults.

I have override variables defined in my component, set with default = null. When needed, I add an explicit value to the override variable when calling layout-master. Inside layout-master, i use the override values if explicitly set to something other than null, otherwise I use the defaults from the document record to define the final layout of the html page.

I have just now worked this out for the first time and it seems to work so far.

Comments

-1

You can try:

public function index()
{
    return View::make('pages/index', array('title' => 'Home page'));
}

Comments

-2
$data['title'] = $this->layout->title = 'The Home Page';
$this->layout->content = View::make('home', $data);

I've done this so far because I needed in both the view and master file. It seems if you don't use $this->layout->title it won't be available in the master layout. Improvements welcome!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.