加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – Laravel Eloquent ORM“whereHas”通过表

发布时间:2020-12-12 08:22:39 所属栏目:MsSql教程 来源:网络整理
导读:嘿,我和Laravel有问题.我尝试通过联系表选择有我选择的城市的地方. 我的模型类: 地方课程: class Places extends Eloquent { public function contacts() { return $this-hasOne('Contacts'); } public function clubs() { return $this-hasOne('Clubs'); }
嘿,我和Laravel有问题.我尝试通过联系表选择有我选择的城市的地方.

我的模型类:
地方课程:

class Places extends Eloquent {
    public function contacts()
    {
        return $this->hasOne('Contacts');
    }
     public function clubs()
     {
        return $this->hasOne('Clubs');
     }    
}

联系人类:

class Contacts extends Eloquent {
     public function cities()
     {
         return $this->hasOne('Cities');
     }
   }

城市类:

class Cities extends Eloquent {

 }

我的查询:

$view->clubs = Places::whereHas('contacts',function ($q) use($city_id){
           $q->where('contacts',function ($q) use($city_id){
               $q->where('id',$city_id);
            });
        })->get();

错误消息:

MySQL server version for the right syntax to use near ‘where id = ?)) >= 1′ at line 1 (SQL: select * from places where (select count(*) from contacts where contacts.places_id = places.id and contacts = (select * where id = 2223)) >= 1)

我知道它缺少“来自”citites,但我不知道如何实现它.

解决方法

您有3个使用关系的选项:

1最直接的解决方案:

Places::whereHas('contacts',function ($q) use ($city_id){
       $q->whereHas('cities',function ($q) use ($city_id){
           $q->where('id',$city_id);
        });
    })->get();

2与上述相同,但使用此PR:https://github.com/laravel/framework/pull/4954

Places::whereHas('contacts.cities',function ($q) use ($city_id){
        $q->where('id',$city_id);   
    })->get();

3使用hasManyThrough关系:

// Place model
public function cities()
{
  return $this->hasManyThrough('City','Contact');
}

// then
Places::whereHas('cities',function ($q) use ($city_id){
   $q->where('cities.id',$city_id);
})->get();

编辑

拥有您的架构显然没有任何建议或您的原始设置可以工作.

这是一个多对多的关系,在Eloquent中属于MajorMany:

// Places model
public function cities()
{
  return $this->belongsToMany('Cities','contacts','places_id','cities_id')
    ->withPivot( .. contacts table fields that you need go here.. );
}

// Cities model
public function places()
{
  return $this->belongsToMany('Places','cities_id','places_id')
    ->withPivot( .. contacts table fields that you need go here.. );
}

然后你可以调用这样的关系:

$city = Cities::first();
$city->places; // collection of Places models

// contacts data for a single city-place pair
$city->places->first()->pivot->open_hours; // or whatever you include in withPivot above

现在,还有另一种设置方法,如果您还需要联系人模型本身:

// Places model
public function contact()
{
  return $this->hasOne('Contacts','places_id');
}

// Contacts model
public function city()
{
  return $this->belongsTo('Cities','cities_id');
}

public function place()
{
  return $this->belongsTo('Places','places_id');
}

// Cities model
public function contact()
{
  return $this->hasOne('Contacts','cities_id');
}

然后:

$city = Cities::first();
$city->contact; // Contacts model
$city->contact->place; // Places model

hasManyThrough在这里根本不起作用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读