我試圖在路由到我的控制器中的 create 函式時傳遞一個變數。
<a href="{{ route('customercolors.create',$customer->id) }}" title="Add color" class="">Add color</a>
客戶顏色控制器.php
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('customer_color.create');
}
我只能通過撰寫自己的路線來解決這個問題,但這不是應該的:
<a href="{{ url('customercolor/add',$customer->id) }}" title="Add color" class="px-4 py-2 bg-gray-800 border border-transparent font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150">Add color</a>
網頁.php
Route::get('/customercolor/add/{customer_id}', function ($customer_id) {
$colors = Color::get();
$customer = Customer::find($customer_id);
return view('customer_color.create', compact('customer', 'colors'));
});
如何使用指定的路線實作這一目標?當我嘗試明顯更新時,我遇到了同樣的問題。
uj5u.com熱心網友回復:
顯然,正如評論中提到的,你把事情搞糊涂了。首先,您有一個從未在路由檔案中使用過的控制器檔案 - web.php. 無意中,您正在使用 PHP 閉包來服務您的視圖。最后,您正在使用一個不存在的命名路由。
進行以下更改以使作業正常:
wep.php
<?php
use Illuminate\Support\Facade\Route;
use App\Http\Controllers\CustomerColorController; // path to your controller
Route::get('/customercolor/add/{customer}', [CustomerColorController::class, 'create'])->name('customercolors.create');
客戶顏色控制器.php
<?php
namespace App\Http\Controllers;
use App\Models\Customer;
use App\Models\Color;
class CustomerColorController extends Controller
{
public function create(Customer $customer)
{
$colors = Color::all(); // assuming you want all color instances
return view('customer_color.create')->with(['customer'=>$customer, 'colors'=>$colors]);
}
}
在 Blade 模板中包含路由
<!-- make sure "$customer" is defined on this page -->
<a href="{{ route('customercolors.create', $customer->id) }}" title="" class="">Add Color</a>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/446172.html
