如何在 Eloquent 模型 / Livewire 组件中添加删除性别的限制?

huangapple go评论60阅读模式
英文:

How do i add Restriction to deletion of a gender in a eloquent model / livewire component?

问题

You can specify that the gender cannot be deleted if its ID is used elsewhere by adding a validation check before deleting the gender. Here's the translated code portion:

public function deleteGender(Gender $gender)
{
    // Check if the gender ID is used in the user entity
    if ($gender->users()->exists()) {
        $this->dispatchBrowserEvent('swal:toast', [
            'background' => 'error',
            'html' => "Cannot delete the gender <b><i>{$gender->gender_type}</i></b> because it is used by some users.",
        ]);
    } else {
        // If the gender ID is not used in the user entity, proceed with deletion
        $gender->delete();
        $this->dispatchBrowserEvent('swal:toast', [
            'background' => 'success',
            'html' => "The gender <b><i>{$gender->gender_type}</i></b> has been deleted",
        ]);
    }
}

This code first checks if the gender ID is used in the user entity. If it is, it displays an error message indicating that the gender cannot be deleted. Otherwise, it proceeds with the deletion and displays a success message.

英文:

I am working on an application that has genders as a standardized entity and the gender ids are used in the user entity. So user1 has gender_id = 1.
How do i specify that if the gender id is used elsewhere, you cannot delete this gender?

I have tried searching online but i didn't find anything that was useful.

public function deleteGender(Gender $gender)
{
    $gender-&gt;delete();
    $this-&gt;dispatchBrowserEvent(&#39;swal:toast&#39;, [
    &#39;background&#39; =&gt; &#39;success&#39;,
    &#39;html&#39; =&gt; &quot;The gender &lt;b&gt;&lt;i&gt;{$gender-&gt;gender_type}&lt;/i&gt;&lt;/b&gt; has been deleted&quot;,
    ]);
}

This is what my current deletion method looks like.

答案1

得分: 0

检查用户是否存在性别,使用 exists 方法,

public function deleteGender(Gender $gender)
{
    $isGenderExists = User::where("gender_id", $gender->id)->exists();
    if ($isGenderExists) {
        $this->dispatchBrowserEvent('swal:toast', [
            'background' => 'error',
            'html' => "您的限制消息在这里....",
        ]);
    } else {
        $gender->delete();
        $this->dispatchBrowserEvent('swal:toast', [
            'background' => 'success',
            'html' => "性别 <b><i>{$gender->gender_type}</i></b> 已被删除",
        ]);
    }
}

请注意,我只翻译了代码中的注释和字符串部分,其他部分保持原样。

英文:

check if gender exist in user or not with help of exists method,

public function deleteGender(Gender $gender)
{
    $isGenderExists = User::where(&quot;gender_id&quot;,$gender-&gt;id)-&gt;exists();
    if($isGenderExists){
        $this-&gt;dispatchBrowserEvent(&#39;swal:toast&#39;, [
            &#39;background&#39; =&gt; &#39;error&#39;,
            &#39;html&#39; =&gt; &quot;your restriction message goes here....&quot;,
        ]);
    }else{
        $gender-&gt;delete();
        $this-&gt;dispatchBrowserEvent(&#39;swal:toast&#39;, [
            &#39;background&#39; =&gt; &#39;success&#39;,
            &#39;html&#39; =&gt; &quot;The gender &lt;b&gt;&lt;i&gt;{$gender-&gt;gender_type}&lt;/i&gt;&lt;/b&gt; has been deleted&quot;,
        ]);
     }
}

答案2

得分: 0

这是关系型数据库专门构建来促进的事情。你不应该尝试在PHP中解决这个问题,除非你在正确的时间获取正确的锁非常小心,否则几乎肯定会引入错误。

相反,你应该通过数据库中的ON DELETE RESTRICT约束(特定的语法可能对你的数据库不同)来解决这个问题,这是唯一保证数据库不会处于无效状态的方法。

英文:

This is the kind of thing a relational database is specifically built to facilitate. You should not attempt to solve this in PHP, where you will almost certainly introduce errors unless you are very careful in obtaining the correct locks at the correct time.

Instead, you should solve this via an ON DELETE RESTRICT (the specific syntax may be different for your database) constraint in the database, which is the only way to guarantee that the database cannot be put into an invalid state.

huangapple
  • 本文由 发表于 2023年5月17日 20:10:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定