user image

Yasen Sayed
Published in : 2022-03-06

Undefined Variable problem on Laravel 9.x

Laravel

 

I'm trying to get a my title variable from my control page and display it on the about page. I don't think I have a typo but it might me I'm not sure.

Here is my control page code;

class PagesController extends Controller
{
    public function index(){
        $title = 'Welcome to laravel';
        return view ('pages.index')->with('title', $title);
    }
    public function about(){
        $title = 'About us';
        return view ('pages.about')->with('title', $title);
    }
    public function services(){
        $title = 'The services';
        return view ('pages.services')->with('title', $title);
    }
}

In this page index and services functions works fine but I can't get the about page.

Here is my display pages;

 

This is Index page

@extends('layouts.app')
@section('content') 
    <h1>{{$title}}</h1>
    <p>This is the Laravel Application</p>
 @endsection

 

This is about page

@extends('layouts.app')
@section('content')
<h1>{{$title}}</h1>
<p>This is the About page</p>
@endsection

The error I have is:

Please let me know if my question is not clear.

 

 

 

Comments

Eslam Zedan Date : 2022-03-06

Best answers

8

Best answers

8

since you are returning just the title therefore there is no need to call any verbs, rather you should directly call the view.

route::view('/about','Pagecontroller@about');

or

pass the parameter by compact.

 return view ('pages.index', compact('title'));

or

return view ('pages.index', ['title' => $title]);

Yasen Sayed Date : 2022-03-06

Thanks, Worked successfully.

Mohamed Atef Date : 2022-03-06

Best answers

46

Best answers

46

you can use 

return view ('pages.index', ['title' => $title]);

this is the best way to send the variable to the blade files

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Laravel redirect to the last location after login
Publish date: 2022-02-21 | Comments: 1
[solved] Class "App\Exceptions\Client" not found
Publish date: 2022-02-09 | Comments: 2
How to pass valiables to 404 error page in laravel?
Publish date: 2022-02-21 | Comments: 2
How to get original attribute value in Laravel?
Publish date: 2022-02-23 | Comments: 3
laravel installation error with composer install
Publish date: 2022-02-23 | Comments: 2