从一对多关系中删除记录

Delete record from one-to-many relationship

我有 3 个表:

用户

id
role
email
typable_id
typable_type

买家

id
name
address
avatar
email
residential_id

住宅

id
name
city 
state

这是我显示关系的模型

User.php

public function typable()
{
  return $this->morphTo();
}

Buyer.php

public function residential()
{
  return $this->belongsTo(Residential::class);
}

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

Residential.php

public function buyer()
{
  return $this->hasMany(Buyer::class);
}

如果我想删除住宅,则需要删除该住宅的所有买家。与删除买家时也需要删除用户一样。我怎样才能做到这一点?这是我的住宅控制器内部的销毁功能。

住宅控制器

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);
    }

我试图将此代码用于删除 destroy() 中的买家(对于尚未使用的用户),但对于要删除的买家,没有任何更改。 $buyers = Buyer::where('residential_id','=',$request->residential_id)->first();$buyers->delete($buyers);

虽然这是我想删除买家时设法执行的代码,但用户也会被删除。

买家控制器

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);
}

希望有好心人帮忙教教我正确的方法

您可能需要注册 model events 来处理:

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();
        });
    }
}

而且您只需删除控制器中的一个对象:

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;
    }
}

方法一

您可以覆盖任何模型的删除功能。

//Residential.php
public function delete()
{
    $this->buyer->delete(); 
    return parent::delete();
}

//Buyer.php
public function delete()
{
    $this->user->delete(); 
    return parent::delete();
}

现在删除住宅记录时,链会先删除相关用户,然后删除买家,最后删除住宅记录。

方法 2

您可以使用each()方法获取所有相关买家,然后获取所有相关用户。

$residentials->buyer
->each(function ($b) {
    $b->user->each(function ($u) {
        $u->delete();
    });
    $b->delete();
});
$residentials->delete();