Laravel laratables 如何在附件中使用 id

我正在使用 Laratables 包。我有一个功能可以在数据表的数据中,但我只需要按 ID 查询用户。


public function data($id) 

{


    return  Laratables::recordsOf(User::class, function($query)

    {

        return $query->where('manager_id', $id)->where('status', 'Enabled');

    });


}

我收到一个错误:


未定义变量:id


我不确定如何在这个函数中使用 id 变量。


繁花不似锦
浏览 121回答 1
1回答

大话西游666

如果您正在使用,php < 7.4那么您可以执行以下操作:public function data($id)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; Laratables::recordsOf(User::class, function($query) use ($id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $query->where('manager_id', $id)->where('status', 'Enabled');&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }如果您正在使用,php 7.4那么您可以为此使用短闭包/箭头功能。&nbsp; &nbsp;public function data($id)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; Laratables::recordsOf(User::class, fn($query) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$query->where('manager_id', $id)->where('status', 'Enabled')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp; }阅读更多anonymous function&nbsp;访问这个或这个
打开App,查看更多内容
随时随地看视频慕课网APP