Laravel 5中whereHas()中的多个orWhere()

偏执狂

接触模型

class Contact extends Model
{
    public function Account()
    {
        return $this->belongsTo('app\Account');
    }
}

帐户模型

class Account extends Model
{
    public function Contact()
    {
        return $this->hasMany('app\Contact');
    }
}

我想查询所有具有account_name = 'test'或的联系人account_city ='test2'

$query = Contact::query();
    $query->whereHas('Account', function ($q) {
        $q->orwhere('account_name' ,'=', 'test');
        $q->orwhere('account_city','=','test2');
    });
$query->get;

此查询向我显示所有具有的联系人,Account但我只想获取具有account_name = 'test'或的联系人account_city ='test2'

的结果 $query->tosql();

select * from `contacts` where exists (select * from `accounts` where (`contacts`.`account_id` = `accounts`.`id` or (`account_name` = ? or `account_city` = ?)) and `accounts`.`deleted_at` is null) and `contacts`.`deleted_at` is null
吉纳尔·索马耶(Jinal Somaiya)

尝试这个:

$query=Contact::query();
$query->whereHas('Account', function ($q) {
    $q->where(function($query) {
       $query->where('account_city','test2')->orWhere('account_name','test');
    });
});
$query->get();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章