Is there a way to determine a page title with a class instead of using Laravel templates @yield and @content?

Viewed 37

Specifically, I want one class, or method, to be taking care of what's going to be in my <title> tag in app.blade.php file (the file included on every page).

3 Answers

You can do this with laravel sessions, firstly you can add a session variable lets say on login like this

Session::put('title', $value);

Now you can change this every time you switch the page via controller, just add this to every request and change the title as you please, and finally in your blades you can just do this

<title>{{ Session::get('title') ?? 'Login' }}</title>

Generally, I use this: In my app.blade.php

<title> {{ $title ? $title.' - ' ? '' }} website-name </title>

And in the controller files I used to return the $data array instead of the compact method:

$data['title'] = 'My title';

return view('view.name', $data);

This will automatically consider the title if you add it to your controller else it will display the website name. No need to add the "<title>" tag in every blade file.

I ended up using and @section('title') in every blade where I need to specify the title.

Related