Laravel 在Relationship表中选择特定字段
我试图在3个不同的表中选择一些字段,我不知道我是否必须使用查询构建器(我宁愿避免使用它),或者我可能在我的模型上犯了错误 这是我的查询代码Laravel 在Relationship表中选择特定字段,laravel,eloquent,Laravel,Eloquent,我试图在3个不同的表中选择一些字段,我不知道我是否必须使用查询构建器(我宁愿避免使用它),或者我可能在我的模型上犯了错误 这是我的查询代码 $lobbies = Lobby::with(array('games' => function($query) { $query->select('id','name'); })) ->with(array('users' => function($query){ $query->select('id', 'p
$lobbies = Lobby::with(array('games' => function($query) {
$query->select('id','name');
}))
->with(array('users' => function($query){
$query->select('id', 'pseudo');
}))
->get();
return response()->json($lobbies);
它返回以下内容:
[
{
"id":2,
"description":"mon premier lobby",
"created_at":"2020-05-13 12:02:36",
"support_link":"https:\/\/discord.gg\/K3NGfv4",
"nb_player":1,
"user_id":1,
"game_id":2,
"games":{
"name":"Counter-strike"
},
"users":{
"id":1,
"pseudo":"misakilou"
}
}
]
但我只需要描述、名称(游戏名称)、nb_播放器和伪字段,而不需要其他字段
这是我的不同型号
用户模型
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\App;
use Illuminate\Database\Eloquent\Model;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'pseudo', 'mail', 'password','active','confirm_password'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function lobbies(){
return $this->hasOne('App\Lobby');
}
public function getUsernameAttribute(){
return $this->pseudo;
}
}
您可以将选择
添加到大厅
模型中,但请记住,您必须选择外键才能使用工作。如果不希望该数据包含在响应中,可以映射查询返回的集合,以便在返回响应之前对其进行自定义。大概是这样的:
$lobbies = Lobby::select('id', 'description', 'nb_player', 'game_id', 'user_id')
->with(array('games' => function($query) {
$query->select('id','name');
}))
->with(array('users' => function($query){
$query->select('id', 'pseudo');
}))
->get();
$response = $lobbies->map(function ($lobby, $key) {
$item = [
'description' => $lobby->description,
'nb_player' => $lobby->nb_player,
'game_name' => $lobby->games->name,
'user_pseudo' => $lobby->users->pseudo,
];
return $item;
});
return response()->json($response);
$lobbies = Lobby::join('users', 'users.id', '=', 'lobbies.user_id')
->join('games', 'games.id', '=', 'lobbies.game_id')
->select('lobbies.description', 'lobbies.nb_player', 'users.pseudo', 'games.name')
->get();
return response()->json($lobbies);
如果要避免在集合上进行后续映射,可以使用联接而不是Eloquent的关系。大概是这样的:
$lobbies = Lobby::select('id', 'description', 'nb_player', 'game_id', 'user_id')
->with(array('games' => function($query) {
$query->select('id','name');
}))
->with(array('users' => function($query){
$query->select('id', 'pseudo');
}))
->get();
$response = $lobbies->map(function ($lobby, $key) {
$item = [
'description' => $lobby->description,
'nb_player' => $lobby->nb_player,
'game_name' => $lobby->games->name,
'user_pseudo' => $lobby->users->pseudo,
];
return $item;
});
return response()->json($response);
$lobbies = Lobby::join('users', 'users.id', '=', 'lobbies.user_id')
->join('games', 'games.id', '=', 'lobbies.game_id')
->select('lobbies.description', 'lobbies.nb_player', 'users.pseudo', 'games.name')
->get();
return response()->json($lobbies);
使用join将在查询到DB时具有更好的性能,并且还可以避免以后的映射。我保留了雄辩的人际关系选项,只是为了遵循你一直在做的事情。