如何在laravel的雄辩关系中使用where子句

时间:2018-03-25 11:33:29

标签: laravel foreign-keys relationship

我正试图在laravel雄辩的关系中访问公司变量,但我得到错误未定义的变量。

控制器

$assignedCases = AddressCaseAssign::with(['addresses' => function ($query) {
                                $query->where('company_id', '=',$request->company);

                            }])->where('user_id',$request->executive)
                            ->get();

模型

public function addresses(){
    return $this->belongsTo(Address::class,'address_id');
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您需要使用$request关键字

在where子句中传递use
$assignedCases = AddressCaseAssign::with(['addresses' => function ($query) use ($request) {
                            $query->where('company_id', '=',$request->company);

                        }])->where('user_id',$request->executive)
                        ->get();