gpt4 book ai didi

php - Laravel whereSlug 不工作

转载 作者:行者123 更新时间:2023-11-29 11:55:30 28 4
gpt4 key购买 nike

当我想在 Laravel 中使用这样的 slug 搜索票证时:

 public function showTicket(Request $slug)
{
$ticket = Ticket::whereSlug($slug)->firstOrFail();
if(Auth::user()->id == $ticket->user_id)
{
return view('users.ticket')->with('ticket',$ticket);
}
}

我收到错误:

> ModelNotFoundException in Builder.php line 157:
No query results for model [App\Ticket].
in Builder.php line 157
at Builder->firstOrFail() in UserTicketsController.php line 66
at UserTicketsController->showTicket(object(Request), '5627caed7b3bb')
at call_user_func_array(array(object(UserTicketsController), 'showTicket'), array(object(Request), 'slug' => '5627caed7b3bb')) in Controller.php line 256
at Controller->callAction('showTicket', array(object(Request), 'slug' => '5627caed7b3bb')) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(UserTicketsController), object(Route), 'showTicket') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))

这个错误:

NotFoundHttpException in Handler.php line 46: No query results for model [App\Ticket]. in Handler.php line 46 at Handler->render(object(Request), object(ModelNotFoundException)) in Kernel.php line 281 at Kernel->renderException(object(Request), object(ModelNotFoundException)) in Kernel.php line 91 at Kernel->handle(object(Request)) in index.php line 54

但是当我这样做时:

$ticket = Ticket::whereId(1)->firstOrFail();

有效吗?所以它可以找到模型。我现在知道可能出了什么问题。当我查看数据库时,当我单击票证时,该 slug 也存在,网址正确,这是我的路线:

Route::get('ticket/{slug?}','UserTicketsController@showTicket');

请帮助我。

最佳答案

如果您使用 Request 类,请尝试以下操作:

$ticket = Ticket::whereSlug($slug->get('id'))->firstOrFail();

如果您传递参数,请勿使用 Request 类型。

public function showTicket($slug)
{
$ticket = Ticket::whereSlug($slug)->firstOrFail();

关于php - Laravel whereSlug 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33265847/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com