gpt4 book ai didi

php - 来自 hasMany 关系的 Laravel 预加载模型

转载 作者:搜寻专家 更新时间:2023-10-31 20:58:12 25 4
gpt4 key购买 nike

有3张表:

用户: id, name

恭维:id, name

users_compliments: id, compliment_id, user_id, complimentor_id

这是我的用户模型:

class User extends Model
public function sent_compliments() {
return
$this->belongsToMany(
'Compliment',
'users_compliments',
'complimentor_id',
'compliment_id'
)->withPivot('user_id');
}

这是一个示例响应,如果我运行 $user->sent_compliments()->get()

Illuminate\Database\Eloquent\Collection {
all: [
Compliment {
id: 2,
name: "You are smart",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {
user_id: 6,
id: 99,
},
},
Compliment {
id: 52,
name: "You are funny",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {
user_id: 42,
id: 186,
},
},
],
}

但这是我想要的响应(它包括实际的用户模型,而不仅仅是数据透视表中的 user_id)

Illuminate\Database\Eloquent\Collection {
all: [
Compliment {
id: 2,
name: "You are smart",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {
user_id: 6,
id: 99,
},
user {
id: 6,
name: "John"
}
},
Compliment {
id: 52,
name: "You are funny",
pivot: Illuminate\Database\Eloquent\Relations\Pivot {
user_id: 42,
id: 186,
},
user {
id: 42,
name: "Alex"
}
},
],
}

有没有一种方法可以通过 Eloquent 急切加载(比如使用 With:: 命令)来获取此数据,而不是遍历数据透视表、收集 user_ids 并手动查询 User 表?

最佳答案

您需要定义pivot 类并添加关系belongsTo 到user_id 的pivot 模型

用户模型

public function sent_compliments()  {  
return
$this->belongsToMany(
'Compliment',
'users_compliments',
'complimentor_id',
'compliment_id'
)->withPivot('user_id')->using('App\UserComplement');
}

枢轴模型 使用 Illuminate\Database\Eloquent\Relations\Pivot;

class UserComplement extends Pivot {
protected $table = 'users_compliments';

public function user(){
return $this->belongsTo('App\User', 'user_id');
}
}

飞思数据

//if you have user instance already then
$complements = $user->sent_compliments;

//if you don't have fetched user yet then try this
$user = User::with('sent_compliments')->find($userId);
$complements = $user->sent_compliments;

foreach($complements as $complement){
dd($complement);
dd($complement->pivot->user);
}

有关详细信息,请查看此定义自定义中间表模型 https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

编辑

正如@JonasStaudenmeir 所指出的,没有办法预先加载pivot user 关系。这是另一种解决方案

用户模型

public function sent_compliments()  {  
return $this->hasMany(UserCompliment::class , 'complimentor_id');
}

用户补充模型

class UserComplement extends Model {
$table = 'users_compliments';

public function complimentor() {
return $this->belongsTo(User::class , 'complimentor_id');
}

public function complement() {
return $this->belongsTo(Compliment::class , 'compliment_id');
}

public function user() {
return $this->belongsTo(User::class , 'user_id');
}
}

获取数据

$user = User::with('sent_compliments','sent_compliments.complement', 'sent_compliments.user')->find($userId);
$sentComplements = $user->sent_compliments;

foreach($sentComplements as $sentComplement){
dd($sentComplement->complement);
dd($sentComplement->user);
}

希望它能让你清醒

关于php - 来自 hasMany 关系的 Laravel 预加载模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51152532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com