2

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

lily
  • 199
  • 2
  • 3
  • 14
  • 2
    are you sure this is the only 'action' that is returning this specific view? – lagbox Sep 10 '20 at 14:28
  • yes. I got no error for the index page and the service page except for this `about` page. My coding for `index` page and `about` page are same. Which is why I dont understand why I got this error. When I have no problem with the index page – lily Sep 10 '20 at 14:32
  • that isn't what I am asking ... do you have any other actions that are returning this view named `'pages.about'`? – lagbox Sep 10 '20 at 14:33
  • oh im sorry. I misunderstood you. No, I dont have any other actions that return to this 'pages.about` – lily Sep 10 '20 at 14:35
  • 1
    if that is the action you are hitting then I don't know why it would be undefined at the moment, sorry – lagbox Sep 10 '20 at 14:36
  • 1
    yeah its fine. I'm kinda confused too right now. Thank you – lily Sep 10 '20 at 14:37
  • did this get resolved? – lagbox Sep 12 '20 at 07:45
  • 1
    Does this answer your question? [How to pass data to view in Laravel?](https://stackoverflow.com/questions/18341792/how-to-pass-data-to-view-in-laravel) – Nico Haase Aug 19 '21 at 15:11

3 Answers3

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

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

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Emtn
  • 21
  • 4
0
php artisan route:cache

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

j-tap
  • 56
  • 3
  • 5
-1

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

Ahmed Ali
  • 33
  • 4
  • Please add some explanation to your answer such that others can learn from it. Why should having a route duplicated cause the given problem? – Nico Haase Apr 06 '23 at 06:16