[英]Delete record from one-to-many relationship
I have 3 tables:我有3张桌子:
users用户
id
role
email
typable_id
typable_type
buyers买家
id
name
address
avatar
email
residential_id
residentials住宅
id
name
city
state
And here is my model that shows the relationship这是我的模型,显示了这种关系
User.php用户名
public function typable()
{
return $this->morphTo();
}
Buyer.php买家.php
public function residential()
{
return $this->belongsTo(Residential::class);
}
public function user()
{
return $this->morphMany(User::class, 'typable');
}
Residential.php住宅.php
public function buyer()
{
return $this->hasMany(Buyer::class);
}
If I want to delete the residential, all buyers from that residential need to be deleted.如果我想删除该住宅,则需要删除该住宅的所有买家。 Same as users need to be deleted too when the buyers is deleted.
与删除买家时也需要删除用户一样。 How can I do that?
我怎样才能做到这一点? This is what insides my Residential Controller for destroy function.
这就是我的住宅控制器内部用于销毁功能的内容。
ResidentialController住宅控制器
public function destroy(Request $request)
{
$residentials = Residential::find($request->input('id'));
$residentials->id = $request->input('id');
$residentials->name = $request->input('name');
$residentials->delete($residentials);
return response()->json($residentials);
}
I have tried to put this code to delete the buyers (for users not yet) inside destroy() but nothing is changed for the buyers to be deleted.我试图将此代码放在destroy() 中删除买家(对于尚未使用的用户),但对于要删除的买家没有任何更改。
$buyers = Buyer::where('residential_id','=',$request->residential_id)->first();
$buyers->delete($buyers);
While this is the code that I managed to do if I want to delete the buyers, the users are deleted too.如果我想删除买家,这是我设法执行的代码,但用户也被删除了。
BuyerController买方控制器
public function destroy(Request $request)
{
$users = User::where('email', '=', $request->email)->first();
$buyers = Buyer::find($request->input('id'));
$buyers->id = $request->input('id');
$buyers->name = $request->input('name');
$buyers->delete($buyers);
$users->delete($users);
return response()->json($buyers);
}
I hope there is someone to help and teach me the correct way.我希望有人可以帮助并教我正确的方法。
You might want to register model events to handle that:您可能想要注册模型事件来处理:
class Residential extends Model
{
// Lets use plural form for a HasMany relationship.
public function buyers()
{
return $this->hasMany(Buyer::class);
}
protected static function booted()
{
static::deleting(function ($user) {
// I am using Higher Order Message, check this out: https://laravel.com/docs/8.x/collections#higher-order-messages
$this->buyers->each->delete();
});
}
}
class Buyer extends Model
{
// Lets use the plural form for a MorpMany relationship.
public function users()
{
return $this->morphMany(User::class, 'typable');
}
protected static function booted()
{
static::deleting(function ($user) {
$this->users->each->delete();
});
}
}
And you only have to remove a single object in your controller:并且您只需要删除控制器中的一个对象:
class ResidentialController
{
public function destroy(Request $request)
{
$residential = Residential::findOrFail($request->input('id'));
$residential->delete();
// The framework is gonna automatically convert this to a JSON object.
return $residential;
}
}
class BuyerController
{
public function destroy(Request $request)
{
$buyer = Buyer::findOrFail($request->input('id'));
$buyer->delete();
// The framework is gonna automatically convert this to a JSON object.
return $buyer;
}
}
Approach-1方法一
you can override the delete function for any model.您可以覆盖任何模型的删除功能。
//Residential.php
public function delete()
{
$this->buyer->delete();
return parent::delete();
}
//Buyer.php
public function delete()
{
$this->user->delete();
return parent::delete();
}
Now when you delete any Residential record, the chain will first delete any related user and then delete buyer and finally delete the Residential record.现在当您删除任何Residential 记录时,链将首先删除任何相关用户,然后删除买家,最后删除Residential 记录。
Approach-2方法二
You can use each()
method to get all relating buyer and then get all relating user.您可以使用
each()
方法获取所有相关买家,然后获取所有相关用户。
$residentials->buyer
->each(function ($b) {
$b->user->each(function ($u) {
$u->delete();
});
$b->delete();
});
$residentials->delete();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.