Laravel Eloquent ORM“whereHas”通过表

时间:2014-08-07 09:40:10

标签: sql laravel orm eloquent

嘿,我对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服务器版本,在第1行的'id =?))> = 1'附近使用正确的语法(SQL:select * from places where(select count(*)来自contacts其中contactsplaces_id = placesidcontacts =(select * where id = 2223)) > = 1)

我知道它“遗漏”来自citites,但我不知道如何实现它。 Model relationships

3 个答案:

答案 0 :(得分:12)

您有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中是belongsToMany

// 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', 'contacts', '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

现在,还有另一种设置方式,以防您需要Contacts模型本身:

// 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根本没有在这里工作

答案 1 :(得分:0)

如果你知道城市id,你可以从中找到相应的地方,你可以从城市开始并回到这个地方。为此,您需要定义关系的反转。

// Add this function to your Cities Model
public function contact()
{
    return $this->belongsTo('Contact');
}


// Add this function to your Contacts Model
public function place()
{
     return $this->belongsTo('Places');
}

现在您可以查询城市并找到地方。

$place = Cities::find($city_id)->contact->place;

修改 在函数中添加了缺失的返回

答案 2 :(得分:-1)

SELECT * from pemeriksaan_ginekologi_iva where id in
(
SELECT m1.id
FROM pemeriksaan_ginekologi_iva m1 LEFT JOIN pemeriksaan_ginekologi_iva m2
 ON (m1.id_klien = m2.id_klien AND m1.id < m2.id)
WHERE m2.id IS NULL
) and id_klien in (select id from klien where id_kelurahan =1)
AND (hasil_periksa='IVA Negatif / Normal')  traslate to laravel