Variable in blade is undefined when passing variable from controller Laravel
P粉056618053
P粉056618053 2023-08-31 15:58:04
0
2
378

So I want to return some string from model and controller but it always says undefined variable, although when I use When checked with dd($ it successfully passes a) and dd($b). What did i do wrong?

about.blade:

@extends('layout.template'); @section('homeContainer'); 

{{ $a }}


{{ $b }}

@endsection

About the controller:

 

About the model:

 

Directions:

 AboutController::info(), ]); });

P粉056618053
P粉056618053

reply all (2)
P粉111641966

The controller never runs, only the callbacks in the web.php file. This means you don't have a and b variables, only a name variable

    P粉194919082

    thank you for your reply! Turns out I mistakenly declared the model as a variable and a route,

    For the route I changed it to

    Route::get('/about',[AboutController::class,'info']);

    For controllers and models I remove the static and change the model declaration

    Controller:

    public function info() { $model = new AboutModel(); $a = $model->Info(); $b = "This data is from controller"; return view('about', compact('a', 'b')); }

    model:

    public function Info(){ $a = "This value is from model"; return $a; }
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!