Php 雄辩的多任务查询

Php 雄辩的多任务查询,php,mysql,laravel,eloquent,twig,Php,Mysql,Laravel,Eloquent,Twig,我试图在php中用Slim和Elount 5.1创建一个搜索函数,但从数据库中获取结果时遇到了一个问题。 这是我的桌子 Listing Table id | title | description | price | size |etc... -----|--------|-------------|-------|--------|------- 1 | list1 | some text | 500 | 50 | 2 | list2 | some t

我试图在php中用Slim和Elount 5.1创建一个搜索函数,但从数据库中获取结果时遇到了一个问题。 这是我的桌子

Listing Table id | title | description | price | size |etc... -----|--------|-------------|-------|--------|------- 1 | list1 | some text | 500 | 50 | 2 | list2 | some text | 700 | 80 | 3 | list3 | some text | 350 | 120 | Listings Option Table id | id_listing | opt_name | opt_value -----|-------------|-------------|---------- 1 | 1 | rooms | 3 2 | 1 | baths | 4 3 | 2 | rooms | 8 4 | 3 | baths | 1 5 | 3 | rooms | 6 Listings Images Table id | id_listing | file_name -----|-------------|------------- 1 | 1 | file_1.png 2 | 1 | file_2.png 1 | 2 | file_3.png 2 | 3 | file_4.png 我的课

//Listing Class

class Listing extends Eloquent{
  protected $table = 'listings';


  public function options(){
    return $this->hasMany('Casas\Listing\ListingOption', 'id_listing', 'id');
  }

  public function imgs(){
    return $this->hasMany('Casas\Listing\ListingImg', 'id_listing');
  }

}

// ListingOption class

class ListingOption extends Eloquent{
  protected $table = 'listings_options';

  public function listings()
  {
    return $this->belongsTo('Casas\Listing\Listing', 'id_listing');
  }

}

// ListingImg Class
class ListingImg extends Eloquent{
  protected $table = 'listings_imgs';

  public function listings()
  {
    return $this->belongsTo('Casas\Listing\Listing', 'id_listing');
  }

}
但是我找不到一种方法来按我想要的方式过滤查询。我试了很多东西

示例:我想获得所有价格低于600且房间数超过2间的房源

这就是清单ID1和3


如果有人能帮助创建此查询,我将不胜感激。

我将尝试以下方法:

$result = $app->listing->with(
    ['options' => function ($query) {
        $query->where('opt_value', '>', 2);
        }
    ], 
    ['imgs'])->where('price', '<', 600)->get();
$result=$app->listing->with(
['options'=>函数($query){
$query->where('opt_value','>',2);
}
], 

['imgs']->where('price','您想要使用的是
whereHas

访问模型的记录时,您可能希望根据关系的存在限制结果

$result=$app->listing->with('options','imgs'))
->whereHas('options',function($query){
$query->where('opt_value','>',2)
->其中('opt_name','room');
})

->where('price',我试过了,但首先,用这个where in options,我不知道它是在查看选项房间还是浴室,并且它同时返回。它返回的列表也没有任何选项。听起来你需要进一步限制结果。你可以简单地链接其他
->where()
子句来获取您所需的内容。我尝试了类似这样的方法,
$listado=$app->listing->with(['options'=>function($query){$query->where('opt_name','rooms')->where('value','>','imgs','2);}],'imgs']->where('price',用(['relation'=>functon(['relation'=>functon('q{}])更改
whereHas('relation',funcon($q){})
语句更多信息非常感谢我对其进行了一些调整,使其完全按照我想要的方式工作。非常感谢@FabioAntunes@abonive我很高兴我帮了你!
$result = $app->listing->with(
    ['options' => function ($query) {
        $query->where('opt_value', '>', 2);
        }
    ], 
    ['imgs'])->where('price', '<', 600)->get();
$result = $app->listing->with('options', 'imgs')
    ->whereHas('options', function ($query) {
        $query->where('opt_value', '>', 2)
            ->where('opt_name', 'room');
    })
    ->where('price', '<', 600)->get();