Skip to content
Advertisement

Cannot passing data to views

I’m using laravel 8 to make pass data to views pages.

Unfortunately i have error saying ‘ Target class [PagesController] does not exist’ on my browser

So here’s my PagesController.php

class PagesController extends Controller
{
public function index (){
$title = 'welcomeee to london';
return view ('pages.index',compact ('title'));
}
}

Web.php

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

index.blade.php

<p> {{$title}}</p>

i want to pass the variable $title to index.blade.php but have error. Tried so many times but didn’t work

Advertisement

Answer

PageController.php

<?php

namespace AppHttpControllers;

use IlluminateFoundationAuthAccessAuthorizesRequests;
use IlluminateFoundationBusDispatchesJobs;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateRoutingController as BaseController;
class PagesController extends Controller
{
public function index (){
$title = 'Laravel 8 is awesome';
return view('index', compact('title'));
}
}

index.blade.php

<p> {{  $title}}</p>

for web.php

<?php
use AppHttpControllersStudInsertController;
use AppHttpControllersPagesController;
use IlluminateSupportFacadesRoute;
Route::get('/index',[PagesController::class,'index']);

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement