如何使用where和关系获得模型集合


How to get a collection of models using where and relationships?

我有一个Order表,其中有一些关系(Status, User),使用它们的id创建它(status_id, user_id)

我需要使用和Eloquent ORM获得Order模型的集合,但我需要通过他们的状态名称(仅在Status表中)和用户名(仅在User表中)

进行过滤

当我在查询生成器中使用'join'时。关系不是水合的,就像下面这个例子:

$emprestimo = DB::table('emprestimos')
            ->join('status', 'status_id', '=', 'status.id')
            ->where('status.nome', 'Solicitado')
            ->where('emprestimos.dono_id', Auth::user()->id)
            ->get();

我如何使用连接进行过滤,并同时水合物我的模型集合?

你可以尝试这样做

看看是否可以

$emprestimo = DB::table('emprestimos')
            ->join('status', function ($join) {
                            $join->on('emprestimos.status_id', '=', 'status.id')
                                 ->where('status.nome', 'Solicitado')
                            })
            ->where('emprestimos.dono_id', Auth::user()->id)
            ->get();