skip to Main Content

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

about.blade:

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

<p> {{ $a }} </p>
<br>
<p>{{ $b }}</p>
@endsection

AboutController :

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsAboutModel;

class AboutController extends Controller
{
    //
    public static function info(){
        $a = AboutModel::info();
        $b = "This data is from controller";
    

        return view('about', compact('a', 'b'));
    }
}

AboutModel :

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class AboutModel extends Model
{
    use HasFactory;
    
    public static function Info(){
        $a = "This value is from model";
        return $a;
    }
}

Routes :

<?php


use IlluminateSupportFacadesRoute;
use AppHttpControllersAboutController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});



Route::get('/about', function () {
    return view('about', [
        "name" => AboutController::info(),
    ]);
});

4

Answers


  1. Chosen as BEST ANSWER

    Thanks for the replies!, turn out I was wrong declarating the model as variable and the routes,

    for the routes I change it to

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

    and for the controller and model 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;
        }
    

  2. the controller never run, only the callback in the web.php file runs.
    this means you do not have a and b variables but only a name variable

    Login or Signup to reply.
  3. Route declaration is not correct. Route can’t handle rendered blade code as return response. Please check below code for web.php file.

    <?php
     use IlluminateSupportFacadesRoute;
     use AppHttpControllersAboutController;
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider and all of them will
    | be assigned to the "web" middleware group. Make something great!
    |
    */
    
    Route::get('/', function () {
       return view('welcome');
    });
     
    Route::get('/about', [AboutController::class, 'info'])->name("info");
    

    Rest code is fine. It will work for you.

    Login or Signup to reply.
  4. In web.php, you should write the following code:

    Route::get('/about',[AboutController::class,'info']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search