在Laravel中如何使用两个条件(AND)删除记录?

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

How to delete record using where 2 conditions (AND) in Laravel?

问题

在我删除记录时,在控制器中,Laravel 逐个删除条件,而不是同时删除两个条件。

我们能否删除已确定的两个条件的记录,而不是删除每个单独的条件的记录。

英文:

When I delete a record, in a controller, laravel deletes the condition one by one. It is not deleting two conditions at once.

Can we delete records for 2 conditions that have been determined, not to delete records for every single condition.

答案1

得分: 4

你可以尝试:

```php
use App\Models\YourModel;

class YourController extends Controller
{
    public function deleteRecords()
    {
        // 定义你的两个条件
        $condition1 = '你的第一个条件';
        $condition2 = '你的第二个条件';

        // 删除满足两个条件的记录
        YourModel::where('column1', '=', $condition1)
                 ->where('column2', '=', $condition2)
                 ->delete();

        return response()->json(['message' => '记录删除成功']);
    }
}

<details>
<summary>英文:</summary>

You can try:

use App\Models\YourModel;

class YourController extends Controller
{
public function deleteRecords()
{
// Define your two conditions
$condition1 = 'your_first_condition';
$condition2 = 'your_second_condition';

    // Delete records that meet both conditions
    YourModel::where(&#39;column1&#39;, &#39;=&#39;, $condition1)
             -&gt;where(&#39;column2&#39;, &#39;=&#39;, $condition2)
             -&gt;delete();

    return response()-&gt;json([&#39;message&#39; =&gt; &#39;Records deleted successfully&#39;]);
}

}


</details>



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

发表评论

匿名网友

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

确定