如何使用查询字符串重定向命名路由



我得到了下面的代码,它运行得很好,但我想用一个查询字符串重定向到路由,其中condition=$vehicle->condition&make={{$vehicle->make}}&model={{$vehicle->model}}

public function update()

{
//I've removed the logic for simplicity

return redirect(route('vehicles.show', $vehicle))->with('flash', 
'Vehicle Edited Successfully');
}

尝试了一些解决方案和一些链接到文档,但仍然无法正常工作。如有任何协助,我们将不胜感激,干杯!

route()帮助程序的第二个参数是参数列表。传入的任何未在路由本身中定义的参数都将作为查询字符串参数附加到url中。

你的代码看起来像:

return redirect(route('vehicles.show', [
$vehicle,
'condition' => $vehicle->condition,
'make' => $vehicle->make,
'model' => $vehicle->model
]))->with('flash', 'Vehicle Edited Successfully');

尝试在重定向方法中连接查询参数

return redirect(
route('vehicles.show', $vehicle) . 
"?condition={$vehicle->condition}&make={$vehicle->make}&model={$vehicle->model}"
)
->with('flash', 'Vehicle Edited Successfully');

连接查询字符串参数时更安全的方法是使用http_build_query()函数,正如@patricus 在评论中指出的那样

$queryString = http_build_query([
'condition' => $vehicle->condition,
'make' => $vehicle->make,
'model' => $vehicle->model
]);
return redirect(route('vehicles.show') . "?{$queryString}")
->with('flash', 'Vehicle Edited Successfully');

最新更新