Undefined variable in blade when passing variable from controller Laravel.

huangapple go评论58阅读模式
英文:

Undefined variable in blade when passing variable from controller Laravel

问题

以下是翻译好的部分:

所以,我想从 modelcontroller 返回一些字符串,但它总是显示未定义变量,尽管当我使用 dd($a)dd($b) 进行检查时它已成功传递。我做错了什么?

about.blade:

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

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

AboutController :


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AboutModel;

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

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

AboutModel :


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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

Routes :


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

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

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

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(&#39;layout.template&#39;);
@section(&#39;homeContainer&#39;);

&lt;p&gt; {{ $a }} &lt;/p&gt;
&lt;br&gt;
&lt;p&gt;{{ $b }}&lt;/p&gt;
@endsection

AboutController :

&lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AboutModel;

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

        return view(&#39;about&#39;, compact(&#39;a&#39;, &#39;b&#39;));
    }
}

AboutModel :

&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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

Routes :

&lt;?php


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

/*
|--------------------------------------------------------------------------
| 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 &quot;web&quot; middleware group. Make something great!
|
*/

Route::get(&#39;/&#39;, function () {
    return view(&#39;welcome&#39;);
});



Route::get(&#39;/about&#39;, function () {
    return view(&#39;about&#39;, [
        &quot;name&quot; =&gt; AboutController::info(),
    ]);
});

答案1

得分: 1

控制器从不运行,只有 web.php 文件中的回调函数在运行。
这意味着你没有 a 和 b 变量,只有一个名字变量。

英文:

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

答案2

得分: 1

路由声明不正确。路由不能处理渲染的 Blade 代码作为返回响应。 请检查以下代码,位于 web.php 文件中。

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;
/*
|--------------------------------------------------------------------------
| 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");

其余的代码没有问题。这将适用于您。

英文:

Route declaration is not correct. Route can't handle rendered blade code as return response. Please check below code for web.php file.

&lt;?php
 use Illuminate\Support\Facades\Route;
 use App\Http\Controllers\AboutController;
/*
|--------------------------------------------------------------------------
| 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 &quot;web&quot; middleware group. Make something great!
|
*/

Route::get(&#39;/&#39;, function () {
   return view(&#39;welcome&#39;);
});
 
Route::get(&#39;/about&#39;, [AboutController::class, &#39;info&#39;])-&gt;name(&quot;info&quot;);

Rest code is fine. It will work for you.

答案3

得分: 1

web.php 中,您应该写以下代码:

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

In web.php, you should write the following code:

Route::get(&#39;/about&#39;,[AboutController::class,&#39;info&#39;]);

答案4

得分: 1

I can help you translate the code part into Chinese:

感谢您的回复!原来我在声明模型为变量和路由方面出错了,

对于路由,我将其更改为

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

对于控制器和模型,我移除了静态修饰符,并更改了模型的声明:

控制器:

public function info()
{
    $model = new AboutModel();
    $a = $model->Info();
    $b = "这个数据来自控制器";
    
    return view('about', compact('a', 'b'));
}

模型:

public function Info()
{
    $a = "这个值来自模型";
    return $a;
}
英文:

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(&#39;/about&#39;,[AboutController::class,&#39;info&#39;]);

and for the controller and model I remove the static and change the model declaration

Controller :

 public function info()
    {
        $model = new AboutModel();
        $a = $model-&gt;Info();
        $b = &quot;This data is from controller&quot;;

        return view(&#39;about&#39;, compact(&#39;a&#39;, &#39;b&#39;));
    }

Model :

public function Info(){
        $a = &quot;This value is from model&quot;;
        return $a;
    }

huangapple
  • 本文由 发表于 2023年3月31日 02:52:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定