【问题标题】:Laravel Eloquent Pivot Tables filtered by pivot column on object collectionLaravel Eloquent Pivot Tables 按对象集合上的数据透视列过滤
【发布时间】:2018-11-24 04:21:27
【问题描述】:

我有一个问题,正在寻找一个好的解决方案。

我有这些数据库表:

  • 游戏对象
  • game_object_attributes
  • game_object_game_object_attribute(枢轴)

GameObject.php:

public function gameObjectAttributes()
    {
        return $this->belongsToMany('App\Models\GameObjectAttribute')->withPivot('value')->withTimestamps();
    }

GameObjectAttribute.php:

public function gameObjects()
    {
        return $this->belongsToMany('App\Models\GameObject');
    }

现在我正在尝试获取 GameObjectType 的所有 GameObject 并使用枢轴列“value”过滤结果。

$this->model->where('name', $gameObjectTypesEnum)->first()->gameObjects()->join('game_object_game_object_attribute', 'game_objects.id', '=', 'game_object_game_object_attribute.game_object_id')->where('value', '=', '4')->get());

我在这里使用了一个连接,但是有没有办法用 eloquent 的关系来做呢?

这将返回所有类型为“部队”的游戏对象:

$this->model->where('name', $gameObjectTypesEnum)->first()->gameObjects()->get();

“gameObjects()”返回给我一个集合,在这里我不能调用类似的东西

$this->model->where('name', $gameObjectTypesEnum)->first()->gameObjects()->withPivot('value', 3)->get();

$this->model->where('name', $gameObjectTypesEnum)->first()->gameObjects()->gameObjectAttributes->wherePivot('value', 3)->get();

在这里,我可以遍历集合“gameObjects()”并使用 foreach 检查枢轴的值 = 3,但必须有比这更好的解决方案。我是 laravel 的新手..

另外

我试着得到

  1. 通过一个 GameObjectType 获取所有 GameObjects => 返回一个集合**

    GameObjectType::where('name', $gameObjectTypesEnum)->first()->gameObjects()

  2. 然后我尝试通过枢轴过滤以仅获取具有给定 GameObjectType 和枢轴值的游戏对象,例如3.

    ->join('game_object_game_object_attribute', 'game_objects.id', '=', 'game_object_game_object_attribute.game_object_id')->where('value', '=', '4')->get( ));

我做错了,或者这不可能用 Eloquent 来做 :-(

提前谢谢大家。

最好的问候

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    如果我没有误会的话,

    ->wherePivot('value',4) 
    

    添加到您的查询是该连接的雄辩方式。

    【讨论】:

    • 我也这么认为。生成的 Sql 是这样的:"select * from "game_objects" where "game_objects"."game_object_type_id" = ? and "game_objects"."game_object_type_id" is not null and "pivot" = ?"与数据透视表没有联系..我不知道为什么...
    • eloquent 可能会也可能不会使用直接匹配的 sql 查询来获取所需的信息。当使用信息时,它可以使用额外的小查询。 (急切加载,延迟加载主题)。但我假设您已经尝试使用带有示例数据的 wherePivot 方法并且无法获得所需的结果。如果是这样的话,我现在没有其他解决方案。
    • 是的,我已经用 wherePivot 和 withPivot ... 进行了测试 :-(
    【解决方案2】:

    你可以这样做

     $gameObjects = GameObject::where('name', $gameObjectTypesEnum)->whereHas('gameObjectAttributes', function($query)
        {
            $query->where('value', 3); 
        })->get();
    

    【讨论】:

    • 我试过你的解决方案:GameObjectType::where('name', $gameObjectTypesEnum)->first()->gameObjects()->with(array('gameObjectAttributes' => function( $query) { $query->where('value', 3); }))->get());这将返回所有游戏对象而不考虑 where value = 3。 SQL:“select * from "game_objects" where "game_objects"."game_object_type_id" = ? and "game_objects"."game_object_type_id" is not null"
    • 成功了!万分感谢!但我不明白为什么 whereHas 工作?
    猜你喜欢
    • 2017-10-17
    • 2021-01-09
    • 1970-01-01
    • 2017-02-28
    • 2014-07-22
    • 2023-03-26
    • 2014-01-22
    • 2018-09-19
    • 2014-01-15
    相关资源
    最近更新 更多