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']);