Php Laravel 7,缺少[Route:/xxxxx]错误所需的参数
我试图将一个值从视图传递给控制器,并在查询中使用该值。我得到丢失的参数错误。这是我的密码。 视图: 控制器:Php Laravel 7,缺少[Route:/xxxxx]错误所需的参数,php,laravel,routes,laravel-7,Php,Laravel,Routes,Laravel 7,我试图将一个值从视图传递给控制器,并在查询中使用该值。我得到丢失的参数错误。这是我的密码。 视图: 控制器: public function index() { $products = DB::table('products') ->join('families', 'products.id_family', '=', 'families.id') ->select('products.name')
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');
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'));
}
当您在路由中传递通配符时,它是这样写的{f_字母}而不是{$f_字母}
其次,在控制器中,您需要在方法中传递通配符作为如下参数
Route::get('/first_letter/{f_letter}','PtoductController@first_letter')
->name('first_letter');
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
}
将您的路线更新到此路线::获取“/first_letter/{f_letter}”PtoductController@first_letter'->命名“第一个字母”为什么{$f字母}?在路由定义中尝试{f_letter},就像观察一样:如果您不想在函数中注入参数,您也可以从请求对象获得它:$letter=request'f_letter';是的,那是另一种方式,但万一他使用表单