[英]Laravel 7, Missing required parameters for [Route: /xxxxx] error
我正在嘗試將值從視圖傳遞給控制器並在查詢中使用該值。 我收到缺少參數錯誤。 這是我的代碼。 看法:
<a href="{{ action('/first_letter', ['f_letter'=>'A']) }}">A</a>
路線:
Route::get('/first_letter/{$f_letter}','PtoductController@first_letter')
->name('first_letter');
控制器:
public function index()
{
$products = DB::table('products')
->join('families', 'products.id_family', '=', 'families.id')
->select('products.name')
->where('products.name', 'like', $f_letter.'%')
->get();
return view('product.index', compact('products'));
}
你有兩個問題
首先,修復你的路線是這樣的
Route::get('/first_letter/{f_letter}','PtoductController@first_letter')
->name('first_letter');
當你在路由中傳遞一個wildcard
,它是這樣寫的{f_letter}
而不是{$f_letter}
其次,在您的控制器中,您需要像這樣將方法中的wildcard
作為parameter
傳遞
public function index($f_letter)
{
$products=DB::table('products')
->join('families', 'products.id_family', '=', 'families.id')
->select('products.name')
->where('products.name', 'like', $f_letter.'%')
->get();
return view('product.index', compact('products'));
}
您可以在此處閱讀有關路由參數的更多信息
最后你可以像這樣讓你a
標簽更清晰
<a href="{{ route('first_letter', 'A') }}">A</a>
首先,您確定控制器名稱是“ PtoductController ”嗎? 不應該叫ProductController嗎?
然后,如果控制器名稱正確,則從控制器調用的鏈接中刪除“$”。
Route::get('/first_letter/{f_letter}', ['uses' => 'PtoductController@first_letter'])
->name('first_letter');
最后,將 $f_letter 作為參數放入“first_letter”函數中。
public function index($f_letter)
{
//your code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.