英文:
Undefined variable in blade when passing variable from controller Laravel
问题
以下是翻译好的部分:
所以,我想从 model
和 controller
返回一些字符串,但它总是显示未定义变量,尽管当我使用 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('layout.template');
@section('homeContainer');
<p> {{ $a }} </p>
<br>
<p>{{ $b }}</p>
@endsection
AboutController :
<?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 = "This data is from controller";
return view('about', compact('a', 'b'));
}
}
AboutModel :
<?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 = "This value is from model";
return $a;
}
}
Routes :
<?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', function () {
return view('about', [
"name" => 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.
<?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");
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('/about',[AboutController::class,'info']);
答案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('/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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论