php Laravel Eloquent 加入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21885618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:30:22  来源:igfitidea点击:

Laravel Eloquent Join

phpjoinlaraveleloquent

提问by Geartz

I'm fairly new with Laravel. I'm still trying to learn it. My question is:

我对 Laravel 相当陌生。我仍在努力学习它。我的问题是:

I have 3 tables named

我有 3 个表命名

  • games
  • game_options
  • game_platforms

    enter image description here

  • 游戏
  • 游戏选项
  • 游戏平台

    在此处输入图片说明

I have 3 Models for those tables

我有这些表的 3 个模型

  • Game Model

    class Game extends Eloquent
    {
        protected $table = 'games';
    
        public function platforms()
        {
            return $this->hasManyThrough('GamePlatform','GameOptions','id','game_id');
        }
    }
    
  • GamePlatform Model

    class GamePlatform extends Eloquent
    {
        protected $table = 'game_platform';
    }
    
  • GameOption Model

    class GameOptions extends Eloquent
    {
        protected $table = 'game_options';
    }
    
  • 游戏模型

    class Game extends Eloquent
    {
        protected $table = 'games';
    
        public function platforms()
        {
            return $this->hasManyThrough('GamePlatform','GameOptions','id','game_id');
        }
    }
    
  • 游戏平台模型

    class GamePlatform extends Eloquent
    {
        protected $table = 'game_platform';
    }
    
  • 游戏选项模型

    class GameOptions extends Eloquent
    {
        protected $table = 'game_options';
    }
    

So when I do

所以当我做

$game = Game::find(1)->platforms;

It only shows,

它只显示,

{"id":1,"platform_id":20,"game_id":1}
{"id":1,"platform_id":21,"game_id":1}
{"id":1,"platform_id":22,"game_id":1}
{"id":1,"platform_id":23,"game_id":1}
{"id":1,"platform_id":24,"game_id":1}

But I need game name and platform names with those ID's. The thing is, I want to do this with eloquent only. I could go with "DB" or oldschool SQL but I want to learn if this way is possible or not.

但是我需要带有这些 ID 的游戏名称和平台名称。问题是,我只想用 eloquent 来做这件事。我可以使用“DB”或老式 SQL,但我想了解这种方式是否可行。

Also I'm looking for better documentation/books for laravel. Most of what I read were only introduce laravel or far too advanced for me.

此外,我正在为 laravel 寻找更好的文档/书籍。我读到的大部分内容只是介绍 Laravel 或对我来说太高级了。

采纳答案by alexrussell

I left a comment earlier about this but now I'm pretty sure it's the answer you're looking for: you should use belongsToManyrather than hasManyThrough. So first, might I suggest you rename your tables and models to follow Laravel's conventions (plural snake_case table names, singular snake_case alphabetical order pivot table names, singular StudlyCaps model names), that way you'll have the following situation:

我早些时候对此发表了评论,但现在我很确定这是您正在寻找的答案:您应该使用belongsToMany而不是hasManyThrough. 所以首先,我是否建议您重命名您的表和模型以遵循 Laravel 的约定(复数 snake_case 表名称、单数蛇皮字母顺序数据透视表名称、单数 StudlyCaps 模型名称),这样您将遇到以下情况:

Tables:

表格:

  • games
    • id
    • name
  • game_option
    • id
    • game_id
    • option_id
  • options
    • id
    • option
    • name
  • 游戏
    • ID
    • 姓名
  • 游戏选项
    • ID
    • 游戏编号
    • option_id
  • 选项
    • ID
    • 选项
    • 姓名

Now you can rewrite your models to conform to the new structure, and use a belongsToMany relationship too:

现在您可以重写您的模型以符合新结构,并且也可以使用belongsToMany 关系:

class Game extends Eloquent
{
    public function platforms()
    {
        return $this->belongsToMany('Option');
    }
}

class Option extends Eloquent
{
    public function platforms()
    {
        return $this->belongsToMany('Game');
    }
}

Note: you don't have to model the pivot table (game_option) unless you store extra data on the pivot.

注意:game_option除非您在数据透视表上存储额外数据,否则您不必对数据透视表 ( )建模。

Now you should be good to get all options for a given game:

现在你应该很高兴获得给定游戏的所有选项:

$options = Game::find(1)->options;

Or if you need to get all platforms (though I am trying to infer meaning of your code here regarding options and platforms):

或者,如果您需要获取所有平台(尽管我试图在此处推断您的代码关于选项和平台的含义):

$platforms = Game::find(1)->options()->whereOption('platform')->get();

回答by AbstractChaos

you can use the withmethod with eloquent

您可以使用with方法与 eloquent

$game = Game::where('id',1)->with('platforms')->get();

Should return you the game and platforms

应该还给你游戏和平台

For documentation I would first start with the documentationprovided (find it to be about 50% complete) and with the apieverything else is covered

对于文档,我首先从提供的文档开始(发现它大约完成了 50%),然后使用api涵盖了其他所有内容

回答by arrigonfr

You would have to model your tables like:

您必须对表格进行建模,例如:

**games**
id
name

**game_options**
id
game_id
name

**game_platform**
id
game_options_id
platform_id  /* which i assume you get from a platform master table */

Then in your Game Class:

然后在你的游戏类中:

class Game extends Eloquent
{
    protected $table = 'games';

    public function platforms()
    {
        return $this->hasManyThrough('GamePlatform','GameOptions','game_id','game_options_id');
    }
}

Now, this would be assuming that Game Platform belongs to Games through Game Options.

现在,这将假设游戏平台通过游戏选项属于游戏。