簡體   English   中英

刪除記錄前檢查Laravel 8中是否存在hasMany關系

[英]Check if hasMany Relationship Exists in Laravel 8 Before Deleting Record

我試圖在刪除之前檢查關系是否存在。 我在網上看到了很多關於此的其他帖子,但似乎無法讓它發揮作用——當我點擊刪除時,它會忽略我的 @if 語句並刪除記錄。

型號/工作訂單.php

...
public function continuations()
    {
        return $this->hasMany(Continuation::class);
    }
...

型號/續。php

...
public function workorder()
    {
        return $this->belongsTo(Workorder::class, 'workorder_id');
    }
...

工單/index.blade.php

...
  <td class="pr-6">
        <a wire:click="deleteWorkorder({{$workorder->id}})"
           role="menuitem">
        </a>
  </td>
...

工單/索引.php

...
public function deleteWorkorder($id)
    {
        $record = Workorder::find($id);
        $conExists = Continuation::where('workorder_id', $record->id)->exists();

        if($conExists){
            session()->flash('failed-message', 'You can\'t delete a workorder that has a Continuation');
            return;
        }
        $record->delete();
        session()->flash('success-message', 'Workorder Deleted Successfully');

    }
...

通過使用 isNotEmpty() 嘗試類似的事情

$workorder= workorder::find($request->id);
            if ($workorder->continuations->isNotEmpty()) {
                return session()->flash('failed-message', 'You can\'t delete a workorder that has a Continuation');
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM