Laravel,如何为关系列使用 where 条件?

2021-12-26 00:00:00 php laravel eloquent

我正在使用 Laravel 并且在使用 Eloquent ORM 时遇到了一个小问题.我可以使用 JOIN 简单地使用 SQL 查询来实现它,但我似乎无法让它与 Eloquent 一起使用!

I'm using Laravel and having a small problem with Eloquent ORM.. I can get this working simply with SQL query using a JOIN but I can't seem to get it working with Eloquent!

这就是我想要的,我有两张表.一个是Restaurants",另一个是Restaurant_Facilities".

This is what I want, I have two tabels. one is 'Restaurants' and other is 'Restaurant_Facilities'.

表格很简单……而且是一对一的关系.就像有一个带有 idnameslug 等的 restaurant 表和另一个名为 restaurant_facilities 的表 带有 idrestaurant_idwifiparking

The tables are simple.. and One-To-One relations. like there is a restaurant table with id, name, slug, etc and another table called restaurant_facilities with id, restaurant_id, wifi, parking, etc

现在我想做的是..加载所有wifi = 1或wifi = 0的餐厅..我怎么能用 Eloquent 做到这一点?我尝试过急切加载、数据透视表、with()、collections(),但似乎没有任何效果!

Now what I want to do is.. load all restaurants which have wifi = 1 or wifi = 0.. How can i do that with Eloquent ? I have tried eager loading, pivot tables, with(), collections() and nothing seems to work!

对于cuisines 的多对多关系,我遇到了同样的问题!我有相同的 restaurant 表和一个 cuisine 表和一个 restaurant_cuisine_connection 表..

The same problem I have for a Many-To-Many relation for cuisines! I have the same restaurant table and a cuisine table and a restaurant_cuisine_connection table..

但是如何使用 ID 将所有餐厅加载到特定美食中?

but how do I load all restaurants inside a specific cuisine using it's ID ?

这行得通.

Cuisine::find(6)->restaurants()->get();

但我想从 Restaurant:: 模型加载它而不是从菜系中加载..因为我有很多条件链接在一起..它用于搜索和过滤/浏览页面.

but I wanna load this from Restaurant:: model not from cuisines.. because I have many conditions chained together.. its for a search and filtering / browse page.

任何想法或方法?我已经为此苦苦挣扎了 3 天,但仍然没有答案.

Any ideas or ways ? I've been struggling with this for 3 days and still no answer.

示例模型:

class Restaurant extends Eloquent {

    protected $table = 'restaurants';

    public function facilities() {
        return $this->hasOne('Facilities'); 
    }
}

class Facilities extends Eloquent {

    protected $table = 'restaurants_facilities';

    public function restaurant() {
        return $this->belongsTo('Restaurant');
    }

}

附:这似乎有效..但这不是雄辩的方式吗?

PS : This seems to be working.. but this is not Eloquent way right ?

Restaurant::leftJoin(
                'cuisine_restaurant', 
                'cuisine_restaurant.restaurant_id', 
                '=', 'restaurants.id'
             )
             ->where('cuisine_id', 16)
               ->get();

还有什么是在没有其他查询的情况下找到具有特定列值的餐馆数量的最佳方法?比如..我必须找到有停车位 = 1 和 wifi = 1 的餐厅总数?

Also what is the best method to find a count of restaurants which have specific column value without another query ? like.. i have to find the total of restaurants which have parking = 1 and wifi = 1 ?

请帮忙解决这个问题.

谢谢.

推荐答案

如果您必须从 Restaurant 模型加载,我认为在此处执行左连接没有任何问题.我可能会将它抽象为我的 Restaurant 模型上的一个方法,如下所示:

I don't see anything wrong with doing the left join here, if you have to load from the Restaurant model. I might abstract it away to a method on my Restaurant model, like so:

class Restaurant extends Eloquent {
    protected $table = 'restaurants'; // will be default in latest L4 beta

    public function facility()
    {
      return $this->hasOne('Facility');
    }

    // Or, better, make public, and inject instance to controller.
    public static function withWifi()
    {
      return static::leftJoin(
        'restaurant_facilities',
        'restaurants.id', '=', 'restaurant_facilities.restaurant_id'
      )->where('wifi', '=', 1);
    }
}

然后,从您的路线:

Route::get('/', function()
{
  return Restaurant::withWifi()->get();
});

在旅途中 - 尚未测试该代码,但我认为它应该可以工作.您可以改为使用带有约束的预加载,但这只会指定工具对象是否为空.除非您指定 where 子句,否则它仍会返回所有餐厅.

On the go - haven't tested that code, but I think it should work. You could instead use eager loading with a constraint, but that will only specify whether the facility object is null or not. It would still return all restaurants, unless you specify a where clause.

(P.S. 我会坚持使用 Facility 的单数形式.注意 hasOne('Facilities') 怎么读不正确?)

(P.S. I'd stick with the singular form of Facility. Notice how hasOne('Facilities') doesn't read correctly?)

相关文章