我正在嘗試將變數從控制器傳遞到視圖,但出現“未定義變數”錯誤。
網頁.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DashboardController;
Route::get('/dashboard', [DashboardController::class, 'index']);
Route::get('/dashboard/{uuid}/download', [DashboardController::class, 'download'])->name('dashboard.download');
儀表板控制器.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Licence;
use App\Models\Download;
use Auth;
public function index($uuid)
{
if (Licence::where('user_id', Auth::id())->exists()) {
$download = Download::where('uuid', $uuid)->firstOrFail();
return view('dashboard.index', compact('download'));
} else {
return redirect('/checkout');
}
}
public function download($uuid)
{
$download = Download::where('uuid', $uuid)->firstOrFail();
$pathToFile = storage_path('app/public/plugin.zip');
return response()->download($pathToFile);
}
儀表板/index.blade.php
<a href="{{ route('dashboard.download', $download->uuid) }}">
任何想法?
uj5u.com熱心網友回復:
DashboardController找不到中的索引函式$uuid。您已經在 中宣告并將引數傳遞$uuid給index函式DashboardController,但$uuid尚未定義。改變
Route::get('/dashboard', [DashboardController::class, 'index']);
至
Route::get('/dashboard/{uuid}',[DashboardController::class, 'index']);
注意:您必須uuid作為引數傳遞給'/dashboard'路由,以便在$uuid訪問路由時通過 url 為變數賦予一個值(定義)。確保專案中索引頁的所有 url 也被修改以適應新的引數(uuid),否則您將收到 404 錯誤。當您訪問
/dashboard/1
$uuid得到 的值1,以此類推。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/520442.html
