【问题标题】:Laravel - How to delete record based on conditionLaravel - 如何根据条件删除记录
【发布时间】:2021-02-17 09:49:52
【问题描述】:

在我的 Laravel-5.8 中,我有这三 (3) 个模型:

参数

class Parameter extends Model
{    
    protected $table = 'parameters';
    protected $primaryKey = 'id';
    protected $fillable = [
                  'max_score',
                  'min_score',
                  'identity_id',
              ];

    public function identity()
    {
        return $this->belongsTo('App\Models\Identity','identity_id');
    }
}

身份

class Identity extends Model
{
    protected $table = 'identity';

    protected $fillable = [
              'id',
              'name',
          ];

    public function goals()
    {
       return $this->hasMany('App\Models\Goal');
    }  

    public function parameter()
    {
       return $this->hasOne(Parameter::class, 'identity_id');
    }    
}

目标

class Goal extends Model
{
    protected $table = 'goals';

    protected $fillable = [
                  'id',
                  'identity_id',
                  'title',
              ]; 

    public function identity()
    {
        return $this->belongsTo('App\Models\Identity','identity_id');
    }        
}

从模型来看,IdentityParameter 中有一个外键(identity_id),IdentityGoal 中也有一个外键(identity_id)。

我在 Identity 中有这个控制器:

public function destroy($id)
{
    try 
    {          
        $identity = Identity::findOrFail($id);
        $identity->delete();
        Session::flash('success', 'Record deleted successfully.');
        return redirect()->back();
    } 
    catch (Exception $exception) {
            Session::flash('error', 'Record delete failed!.');
            return redirect()->back();
    }       
}   

我希望用户根据这些条件删除Identity记录:

  1. 当用户尝试删除Identity时,应用程序应检查Parameter表并删除存在identity_id外键的记录。

  2. 其次,如果Goal表中存在identity_id的记录,应用程序应该阻止删除。

如何调整public function destroy($id) 来实现这一点?

【问题讨论】:

  • 您可以将这两个条件都放在数据库外键声明ON DELERE CASCADEON DELETE RESTRICT 中。如果您想在代码中使用它,请将您迄今为止尝试过的代码放入其中,以便我们帮助您了解哪些不起作用。
  • 这是atomic 交易吗?即使有goal关系,也应该删除parameter关系?

标签: laravel


【解决方案1】:

您可以在“Identify”模型中添加类似“deleteAll()”的函数,并使用如下关系删除参数:

class Identity extends Model
{
    protected $table = 'identity';

    protected $fillable = [
        'id',
        'name',
    ];

    public function goals()
    {
        return $this->hasMany('App\Models\Goal');
    }

    public function parameter()
    {
        return $this->hasOne(Parameter::class, 'identity_id');
    }

    public function deleteAll(){
        if(!$this->goals){
            $this->parameter->delete();
            return parent::delete();
        }
        return;
    }
}

【讨论】:

  • 我现在怎么称呼这个:deleteAll() in controller
  • 就像任何其他关系一样...... $identity = Identity::findOrFail($id); $identity->deleteAll(); @mikefolu
【解决方案2】:

首先在 identity 的迁移中你需要做的事情

$table->foreign('identity_id')->references('id')->on('parameters')->onDelete('cascade');

因此,当您删除 Identity 时,相关的 Parameter 将在数据库级别自动删除。 为防止删除(您提到的 2 种情况),您需要像这样检查

$identity = Identity::with('goals')->findOrFail($id);
if($identity->goals){
   // you can throw some error here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2023-01-12
    • 2019-04-04
    相关资源
    最近更新 更多