英文:
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('column1', '=', $condition1)
->where('column2', '=', $condition2)
->delete();
return response()->json(['message' => 'Records deleted successfully']);
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论