HasOne laravel在哪里条件 [英] where condition with HasOne laravel

查看:406
本文介绍了HasOne laravel在哪里条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在登录模型中,我与图片表实现了联系

In login model I have implement relation with picture table

function picture () {
   return $this->hasOne('App\Picture');
}

现在我想要Picture.picture_status = 1和User.user_status = 1的数据

Now I want data where Picture.picture_status = 1 and User.user_status = 1

Login::with('picture')->where('picture_status', 1)->where('user_status',1);

但是条件不适用于图片表,如何在两个表上实现和条件

but where condition is not working with picture table, how can i implement and condition on both table

推荐答案

这应该做到:

Login::with(['picture' => function ($query) {
    $query->where('picture_status', 1)->where('user_status',1);
}])->get();

有时您可能希望热切加载关系,但同时为热切加载查询指定其他查询约束 https://laravel.com/docs/5.2/eloquent-relationships#constraining-eager-loads

Sometimes you may wish to eager load a relationship, but also specify additional query constraints for the eager loading query https://laravel.com/docs/5.2/eloquent-relationships#constraining-eager-loads

这篇关于HasOne laravel在哪里条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆