lily
lily

Reputation: 199

Undefined variable in Laravel 8

I have issue with the undefined variable in Laravel 8, I actually don't understand why this happen because I was trying and playing with the Laravel 8.

I created the same method and same coding but somehow when I tried to run the coding for page about I got the error that said

ErrorException Undefined variable: about (View: D:\Server\htdocs\app\resources\views\pages\about.blade.php)

Why is this happening ? I don't understand. Because I use the exact same coding for my other pages and it works but when I try to open the about page it suddenly give me the error when other pages are perfectly fine with no error.

PagesController

class PagesController extends Controller
{
    public function index()
    {   
        $title = "Welcome to my blog";
        // return view ('pages.index', compact('title')); // first method 

        return view ('pages.index')->with('title',$title); // 2 method 
    }

    public function about()
    {
        $about = "About Page";
        return view ('pages.about')->with('about',$about);
    }
    
    public function services()
    {
        $data = array(
            'title' =>'Services' // array
        );
        return view ('pages.service')-> with($data);
    }
    
    
}

about.blade.php


@extends('layouts.app')
 
 @section('content')
        <h1>{{$about}} </h1>
        <p> This is about pages </p>
@endsection

index.blade.php

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

Just to show you the same coding i use for index and about

My route if anyone asking

Route::get('/', [PagesController::class,'index']);
Route::get('/about', [PagesController::class,'about']);
Route::get('/services', [PagesController::class,'services']);

index.blade.php enter image description here

about.blade.php enter image description here

Upvotes: 2

Views: 18691

Answers (3)

Ahmed Ali
Ahmed Ali

Reputation: 43

for people still suffering from this issue , head over to web.php and make sure you dont have a duplicated route.

Upvotes: -1

j-tap
j-tap

Reputation: 56

php artisan route:cache

Clearing the route cache helped me. My variable was added later and data was output from the cache

Upvotes: 0

Emtn
Emtn

Reputation: 21

public function about ()
{
    $ about = "About Page";
    return view ('pages.about') -> compact ('about');
}

Replace it as is and try it: your error will be solved.

Upvotes: 1

Related Questions