在Eloquent Laravel 5.2中加入2个表 - 如何从两个表中检索所有数据?

时间:2023-02-10 02:31:19

I have two tables
1.

我有两张桌子1。

Game Console
      -- console_id
      -- console_name

2.

 Game Labels
     -- game_label_id
     -- console_id (foreign key)
     -- title
     -- description 
     -- image
     -- created

GameConsole Model

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class GameConsole extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public $timestamps = false;
    protected $table = 'console';
    protected $fillable = array('console_name', 'description', 'created');
    protected $primaryKey = 'console_id';

    public function labels()
    {
        return $this->hasMany('App\Http\Models\GameLabel','console_id');
    }

}

GameLabel Model

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class GameLabel extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public $timestamps = false;
    protected $table = 'game_label';
    protected $fillable = array('game_label_id','console_id', 'title','description','image', 'release_date','status','created');
    protected $primaryKey = 'game_label_id';

    public function console()
    {
        return $this->belongsTo('App\Http\Models\GameConsole','console_id');
    }
}

I write this query to get all game labels with console_name

我编写此查询以使用console_name获取所有游戏标签

GameLabel::with('console')->get();

But I am only getting records from game_label table, not from console table.

但我只从game_label表中获取记录,而不是从控制台表中获取记录。

Can any body please tell me that what query I have to write to get all records? Please don't suggest me about query builder joins. I don't want to use that.

可以告诉我,我必须写什么查询来获取所有记录?请不要向我推荐有关查询构建器连接的信息。我不想用它。

2 个解决方案

#1


1  

       namespace App\Http\Models;

        use Illuminate\Database\Eloquent\Model;

        class GameLabel extends Model
        {
            /**
             * The attributes that are mass assignable.
             *
             * @var array
             */
            public $timestamps = false;
            protected $table = 'game_label';
            protected $fillable = array('game_label_id','console_id', 'title','description','image', 'release_date','status','created');
            protected $primaryKey = 'game_label_id';

            public function console()
            {
               return $this->belongsTo('App\Http\Models\GameConsole', 'console_id', 'console_id');
           }
}

in belongs to first console_id represent Game Console table id and and second console_id represent game_label table console_id

in属于第一个console_id表示Game Console表id,第二个console_id表示game_label表console_id

now in controller

现在在控制器中

GameLabel::with('console')->get();

i think all data will be availbale in array under console key

我认为所有数据都将在控制台密钥下以数组形式提供

#2


0  

Yes, its under console key. I found the solution. the console name was not getting in the view so get the console name in view like this

是的,它在控制台键下。我找到了解决方案。控制台名称未进入视图,因此请在视图中获取控制台名称

foreach($game_label as $getGameLabel){
    echo $getGameLabel->console->console_name;
}

#1


1  

       namespace App\Http\Models;

        use Illuminate\Database\Eloquent\Model;

        class GameLabel extends Model
        {
            /**
             * The attributes that are mass assignable.
             *
             * @var array
             */
            public $timestamps = false;
            protected $table = 'game_label';
            protected $fillable = array('game_label_id','console_id', 'title','description','image', 'release_date','status','created');
            protected $primaryKey = 'game_label_id';

            public function console()
            {
               return $this->belongsTo('App\Http\Models\GameConsole', 'console_id', 'console_id');
           }
}

in belongs to first console_id represent Game Console table id and and second console_id represent game_label table console_id

in属于第一个console_id表示Game Console表id,第二个console_id表示game_label表console_id

now in controller

现在在控制器中

GameLabel::with('console')->get();

i think all data will be availbale in array under console key

我认为所有数据都将在控制台密钥下以数组形式提供

#2


0  

Yes, its under console key. I found the solution. the console name was not getting in the view so get the console name in view like this

是的,它在控制台键下。我找到了解决方案。控制台名称未进入视图,因此请在视图中获取控制台名称

foreach($game_label as $getGameLabel){
    echo $getGameLabel->console->console_name;
}