Laravel雄辩的多对多查询

Laravel雄辩的多对多查询,laravel,eloquent,Laravel,Eloquent,请帮我用雄辩的语言提出这个问题。一个企业可以有许多类别 SELECT b.* FROM businesses b INNER JOIN categorybusiness cb ON b.id = cb.business_id INNER JOIN category c ON cb.category_id = c.id WHERE b.location LIKE '%query1%'

请帮我用雄辩的语言提出这个问题。一个企业可以有许多类别

SELECT b.* 
FROM   businesses b 
       INNER JOIN categorybusiness cb 
               ON b.id = cb.business_id 
       INNER JOIN category c 
               ON cb.category_id = c.id 
WHERE  b.location LIKE '%query1%' 
       AND b.location LIKE '%query2%' 
       AND c.name LIKE '%query3%' 
       AND c.name LIKE '%query4%' 
我的桌子是。。业务-包含类别和业务的位置列和数据透视表

更新: 所以我用了这个查询

$business5 = Business::WhereHas('categories', function($q) use($category,$query1)
    {
          $q->whereRaw("name like '%$category%' or businesses.name like '%$category%' $query1");         
    })->get();
$query1看起来像这样,但处于循环中

     $query1 .= " and businesses.address1 like '%$string%'"; 

它工作正常,但有人能帮我从中用雄辩的语言做出“匹配”陈述吗。

要进行雄辩的
查询,您需要设置关系并创建
多对多
关系,您需要在两种模型中建立这样的关系:

业务
模型:

class Business extends Eloquent {
    //...
    public function categories()
    {
        return $this->belongsToMany('Caregory');
    }
}
class Category extends Eloquent {
    //...
    public function businesses()
    {
        return $this->belongsToMany('Business');
    }
}
类别
型号:

class Business extends Eloquent {
    //...
    public function categories()
    {
        return $this->belongsToMany('Caregory');
    }
}
class Category extends Eloquent {
    //...
    public function businesses()
    {
        return $this->belongsToMany('Business');
    }
}
elount
查询(您已经有了一个
pivot
表):

要检查结果,只需使用
dd($businesss)
并检查集合,以便了解如何在
视图中循环它们。基本上,
$business
将包含一个集合,集合中的每个
$business
模型将包含另一个
$categories
集合,因此循环可以是这样的:

@foreach($businesses as $business)
    {{ $business->propertyname }}
    @foreach($business->categories as $category)
        {{ $category->propertyname }}
    @endforeach
@endforeach

要进行有说服力的
查询,您需要设置关系,要创建
多对多
关系,您需要在两种模型中建立如下关系:

业务
模型:

class Business extends Eloquent {
    //...
    public function categories()
    {
        return $this->belongsToMany('Caregory');
    }
}
class Category extends Eloquent {
    //...
    public function businesses()
    {
        return $this->belongsToMany('Business');
    }
}
类别
型号:

class Business extends Eloquent {
    //...
    public function categories()
    {
        return $this->belongsToMany('Caregory');
    }
}
class Category extends Eloquent {
    //...
    public function businesses()
    {
        return $this->belongsToMany('Business');
    }
}
elount
查询(您已经有了一个
pivot
表):

要检查结果,只需使用
dd($businesss)
并检查集合,以便了解如何在
视图中循环它们。基本上,
$business
将包含一个集合,集合中的每个
$business
模型将包含另一个
$categories
集合,因此循环可以是这样的:

@foreach($businesses as $business)
    {{ $business->propertyname }}
    @foreach($business->categories as $category)
        {{ $category->propertyname }}
    @endforeach
@endforeach

假设您有
Business
moel设置,这正是您想要的查询,因为@WereWolf建议的快速加载在这里无法完成任务(其中连接表上的子句与两个单独查询上的子句):


还有另一种方法使用
whereHas
方法,只要您正确设置了
belongTomany
关系

Business::whereHas('categories', function ($q) use ($query3, $query4) {
    $q->where('categories.name', 'like', "%$query3%")
      ->where('categories.name', 'like', "%$query4%");
})->where('businesses.location', 'like', "%$query1%")
    ->where('businesses.location', 'like', "%$query2%")
    ->get();

假设您有
Business
moel设置,这正是您想要的查询,因为@WereWolf建议的快速加载在这里无法完成任务(其中连接表上的子句与两个单独查询上的子句):


还有另一种方法使用
whereHas
方法,只要您正确设置了
belongTomany
关系

Business::whereHas('categories', function ($q) use ($query3, $query4) {
    $q->where('categories.name', 'like', "%$query3%")
      ->where('categories.name', 'like', "%$query4%");
})->where('businesses.location', 'like', "%$query1%")
    ->where('businesses.location', 'like', "%$query2%")
    ->get();

你试过什么?你能粘贴它吗?你试过什么?你能粘贴它吗?有很多好的和简单的方法来实现你的目标与雄辩,有些方法仍然缺乏这些。无论如何,对于您现在想要的东西,请注意,
with()
(急切加载)不是一种方法,因为它会得到比您想要的更多的结果,因为它会检查两个单独查询的约束条件,而不是一次检查所有的约束条件。通过雄辩的,一些方法仍然缺失。无论如何,对于您现在想要的东西,请注意,
with()
(急切加载)不是一种方法,因为它会得到比您想要的更多的结果,因为它会检查两个单独查询上的约束,而不是一次检查所有查询。