Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 拉威尔通过表格口若悬河地说出“Where has”_Sql_Laravel_Orm_Eloquent - Fatal编程技术网

Sql 拉威尔通过表格口若悬河地说出“Where has”

Sql 拉威尔通过表格口若悬河地说出“Where has”,sql,laravel,orm,eloquent,Sql,Laravel,Orm,Eloquent,嘿,我的网站上有一个拉威尔的问题。我试着通过联系表选择有我选择的城市的地方 我的模型课: 学额及班级: class Places extends Eloquent { public function contacts() { return $this->hasOne('Contacts'); } public function clubs() { return $this->hasOne('Clubs');

嘿,我的网站上有一个拉威尔的问题。我试着通过联系表选择有我选择的城市的地方

我的模型课: 学额及班级:

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服务器版本,以便在“where id=?>”附近使用正确的语法1'在第1行SQL:select*from places where select count*from contacts where contacts.places_id=places.id and contacts=select*where id=2223>=1

我知道citites没有做到这一点,但我不知道如何做到这一点。

由于您知道城市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;
编辑:
在函数中添加了缺少的返回

使用关系有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同上,但使用本请购单:

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();
编辑 有了您的模式,很明显,您的建议或原始设置都无法工作

这是一种多对多的关系,它雄辩地属于:

然后您可以这样调用关系:

$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');
}
然后:


hasManyThrough在这里根本不起作用

一个地方只有一个联系人,而一个联系人只有一个城市,因此一个地方只有一个城市,这是正确的吗?我尝试了这个方法并得到了这个错误:Relationship方法必须返回一个类型为Illumb\Database\Eloquent\Relations\Relations的对象这在您的情况下也会起作用,但是@rmclood示例中缺少返回。只需将return语句添加到这两个方法中,它就会工作。我在开始时尝试像选项2一样这样做,但我得到了以下错误:调用未定义的方法illumb\Database\Query\Builder::contacts.cities,尽管我在类contacts函数中有:public function cities。为什么?@PiotrSuchanek,就像我写的那样,这可以通过我链接的PR实现。目前它在雄辩的辩论中不受支持。你可以暂时用1st,也可以用我的PR。嗨,我尝试了这两种方法,但得到了一个类似的错误:“on”子句中的未知列“cities.contacts\u id”SQL:select*from places where select count*from cities inner join contacts.id=cities.contacts\u id where places.id=contacts\u id”和cities.id=2223>=1。表Cities没有列contacts\u id。Places表有列contacts\u id。有解决方案吗?外键名是什么?你有复数的型号名称,这不是雄辩的期望。这就是为什么它尝试猜测键,因为您没有在关系中指定它并查找联系人id,而我认为它是联系人id。我添加了以下内容:return$this->hasManyThrough'Cities'、'contacts'、'Cities_id'、'contacts_id';仍然会得到同样的错误。我还在我的问题中添加了图片,这可能会有所帮助。如果你能解释一下这是如何解决问题的,这个答案会有很大的改进:
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
// 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
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