Laravel:Return 具有附加枢轴数据的更新模型
Laravel: Return the updated model with additional pivot data
我正在成功更新数据透视表 table 中的数据。我现在正在尝试 return 更新的模型 - 包括刚刚更新的数据透视表。
我能够 return 模型,但不确定如何从那里获取数据透视表。
这是我的控制器:
public function update(Request $request, Role $role)
$user->roles()->updateExistingPivot($attributes['id'], $attributes); // 200 status
...
return response()->json(['role' => $role], 200);
// have also tried this - and does return the value, but would like the whole object if possible.
$active = $user->roles->find($attributes['id'])->pivot->active;
}
这是我的 User
模型:
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id')
->withPivot('active') // this is the column I am updating
->withTimestamps();
}
以上一切正常。我的数据正在更新,我的数据透视表中的时间戳也在更新 table。
我(认为)遇到的问题是我正在 return 传递传入的相同 $role
。我正在使用 vue 组件来呈现结果,所以我我想使用从我的数据库返回的实际数据。
即使我使用查询查找 Role
,我也只是取回模型;不包括我想阅读的数据透视列。
我正在这样检查我的 vue 组件中的响应:
console.log('response.data', response.data);
response.data {
role:
created_at: "..."
title: "foo"
...
}
上面呈现了相同的模型对象(没有数据透视列)。如何将我的 active
数据透视列附加到响应数据?感谢您的建议!
解决方案
感谢 matticustard 的帮助。这正是我需要指向正确方向的推动力。这是我的代码,希望它能帮助其他人。
控制器
$public function update(Request $request, Role $role)
{
$attributes = request()->validate([
'active' => 'required',
'id' => 'required',
]);
$user = auth()->user();
$user->roles()->updateExistingPivot($attributes['id'], $attributes);
// Return the role using the relationship.
$role_with_pivot = $user->roles()->where('role_id', $attributes['id'])->first();
return response()->json(['role' => $role_with_pivot], 200);
}
以上将 return 这是更新我的数据库后的响应:
role:
created_at: "..."
icon: "..."
id: 1
pivot:
active: true
role_id: 1
user_id: 1
...
title: "foo"
...
vue组件
<template>
<div class="role" :class="{'active': active}" @click="handleClick($event, role)">
<div v-if="role.icon" class="role-icon">
<i :class="role.icon"></i>
</div>
<div class="role-title">
{{ role.title }}
</div>
</div>
</template>
...
data() {
return {
active: this.role.pivot.active,
}
},
...
methods: {
async handleClick(event, role) {
try {
let response = await axios.patch(`/my-path/${role.id}`, {
active: !this.active,
id: role.id,
});
if (response.status === 200) {
console.log('response.data', response.data);
this.active = response.data.role.pivot.active;
console.log('active: ', this.active);
} else {
console.error('Error: could not update role. ', response);
}
} catch (error) {
console.error('Error: sending patch request. ', error);
}
},
},
仅在加载对应关系时才加载数据透视表。否则,不会执行触摸枢轴 table 的查询。默认情况下 # Route Model Binding,仅检索模型。
您将需要明确执行关系驱动的查找。
// load the role using the relationship
$role_with_pivot = $user->roles()->where('id', $role->id)->first();
// in some circumstances, it may be necessary to use the full table name
// $role_with_pivot = $user->roles()->where('roles.id', $role->id)->first();
return response()->json(['role' => $role_with_pivot], 200);
然后您应该能够访问响应中的数据透视表。
let active = response.data.role.pivot.active;
我正在成功更新数据透视表 table 中的数据。我现在正在尝试 return 更新的模型 - 包括刚刚更新的数据透视表。
我能够 return 模型,但不确定如何从那里获取数据透视表。
这是我的控制器:
public function update(Request $request, Role $role)
$user->roles()->updateExistingPivot($attributes['id'], $attributes); // 200 status
...
return response()->json(['role' => $role], 200);
// have also tried this - and does return the value, but would like the whole object if possible.
$active = $user->roles->find($attributes['id'])->pivot->active;
}
这是我的 User
模型:
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id')
->withPivot('active') // this is the column I am updating
->withTimestamps();
}
以上一切正常。我的数据正在更新,我的数据透视表中的时间戳也在更新 table。
我(认为)遇到的问题是我正在 return 传递传入的相同 $role
。我正在使用 vue 组件来呈现结果,所以我我想使用从我的数据库返回的实际数据。
即使我使用查询查找 Role
,我也只是取回模型;不包括我想阅读的数据透视列。
我正在这样检查我的 vue 组件中的响应:
console.log('response.data', response.data);
response.data {
role:
created_at: "..."
title: "foo"
...
}
上面呈现了相同的模型对象(没有数据透视列)。如何将我的 active
数据透视列附加到响应数据?感谢您的建议!
解决方案
感谢 matticustard 的帮助。这正是我需要指向正确方向的推动力。这是我的代码,希望它能帮助其他人。
控制器
$public function update(Request $request, Role $role)
{
$attributes = request()->validate([
'active' => 'required',
'id' => 'required',
]);
$user = auth()->user();
$user->roles()->updateExistingPivot($attributes['id'], $attributes);
// Return the role using the relationship.
$role_with_pivot = $user->roles()->where('role_id', $attributes['id'])->first();
return response()->json(['role' => $role_with_pivot], 200);
}
以上将 return 这是更新我的数据库后的响应:
role:
created_at: "..."
icon: "..."
id: 1
pivot:
active: true
role_id: 1
user_id: 1
...
title: "foo"
...
vue组件
<template>
<div class="role" :class="{'active': active}" @click="handleClick($event, role)">
<div v-if="role.icon" class="role-icon">
<i :class="role.icon"></i>
</div>
<div class="role-title">
{{ role.title }}
</div>
</div>
</template>
...
data() {
return {
active: this.role.pivot.active,
}
},
...
methods: {
async handleClick(event, role) {
try {
let response = await axios.patch(`/my-path/${role.id}`, {
active: !this.active,
id: role.id,
});
if (response.status === 200) {
console.log('response.data', response.data);
this.active = response.data.role.pivot.active;
console.log('active: ', this.active);
} else {
console.error('Error: could not update role. ', response);
}
} catch (error) {
console.error('Error: sending patch request. ', error);
}
},
},
仅在加载对应关系时才加载数据透视表。否则,不会执行触摸枢轴 table 的查询。默认情况下 # Route Model Binding,仅检索模型。
您将需要明确执行关系驱动的查找。
// load the role using the relationship
$role_with_pivot = $user->roles()->where('id', $role->id)->first();
// in some circumstances, it may be necessary to use the full table name
// $role_with_pivot = $user->roles()->where('roles.id', $role->id)->first();
return response()->json(['role' => $role_with_pivot], 200);
然后您应该能够访问响应中的数据透视表。
let active = response.data.role.pivot.active;