Lavarel 将数据传递到刀片时出错 - 未定义变量

问题描述 投票:0回答:1

我通过 Breeze 的一个小项目发现了 Laravel 来管理身份验证。由于我是全新的,我正在处理一个基本问题,即在仪表板上简单地显示数据。

这是我的代码。仪表板向经过身份验证的用户显示。

“路线”文件夹中的路线:

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MessagesController;
use App\Models\Message;

Route::get('/dashboard', function () {
    return view('dashboard', [MessageController::class, 'index']);
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';

来自

app/Http/Controllers
的控制器:

namespace App\Http\Controllers;

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

class MessagesController extends Controller
{
    /**
     * Display a listing of the resource.
     */    
     
    public function index()
    {
        $messages = Message::all();
        return view('dashboard',compact('messages'));
    }
(...)
}

以及资源/视图中的仪表板(主要摘录):

<div class="w-5/6 py-10">
    @foreach ($messages as $message)
    <div class="m-auto">
        <h3 class="text-gray-700 text-5xl">
        De : {{$message->id_user}}
        </h3>
        <p class="text-lg text-gray-700 py-6">
        {{$message->comment}}
        </p>
        <span class="uppercase text-blue-500 font-bold text -xs italic">
        {{$message->created_at}}
        </span>
    </div>
    @endforeach
</div>

我检查了文档和几个教程/视频,我发现了几种可能的语法来从控制器传递数组,但我测试的语法都不适合我, $message 变量未知。 我必须使用中间件来管理某些东西吗?还是来自其他部分?

谢谢您的帮助。

laravel-blade laravel-breeze
1个回答
0
投票

将您的代码更改为

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MessagesController;

Route::get('/dashboard', [MessagesController::class, 'index'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';

==============================

namespace App\Http\Controllers;

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

class MessagesController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $messages = Message::all();
        return view('dashboard', compact('messages'));
    }
}

======================

 <div class="w-5/6 py-10">
    @foreach ($messages as $message)
    <div class="m-auto">
        <h3 class="text-gray-700 text-5xl">
        De : {{$message->id_user}}
        </h3>
        <p class="text-lg text-gray-700 py-6">
        {{$message->comment}}
        </p>
        <span class="uppercase text-blue-500 font-bold text-xs italic">
        {{$message->created_at}}
        </span>
    </div>
    @endforeach
</div>
© www.soinside.com 2019 - 2024. All rights reserved.