我想删除通过url传递的多个字符串数据,用‘,’分隔.

场景是:

http://127.0.0.1:8888/remove/lui,kui

通过此路径,将删除数据库中的名称字段值的Lui和Kui数据.

我的web.php号文件

Route::get("/remove/{name}",[MyController::class,"remove"]);

MyController.php行文件

use App\Models\City;   
function remove($name){

}

谢谢,你的建议会很有帮助的.

推荐答案

You can accomplish this with the following:

//The string of names
$names = "cat,cat,cat,mouse,dog,rabbit";

//We break it down, seperate them by comma, then add them to an array
//After we will get the unique values only of that array
$arrayOfNames = array_unique(explode(',', $names));
    
//search in the name column for any of these values
//delete if found
//note that you can change the word "name" to any other column name
City::whereIn('name', $arrayOfNames)->delete();

如果你在你的模型中有软删除特征,并想硬删除它:https://laravel.com/docs/9.x/eloquent#soft-deleting

//search in the name column for any of these values
//hard delete if found
City::whereIn('name', $arrayOfNames)->forceDelete();

如果您将来对此感兴趣,也可以进行更新:

//search in the name column for any of these values
//update if found
City::whereIn('name', $arrayOfNames)->update(['age' => 123, 'updated_at' => now()]);

希望它对您有很好的效果:)

Laravel相关问答推荐

在GitHub操作中缺少PHPStan和PHPMD输出,但在本地存在

在Laravel Livewire中,如果一个输入基于另一个唯一的字段是唯一的,如何判断验证?

如何在数据来自Rest API的flutter中创建下拉类别名称列表?

Laravel init 查询多种用途

如何在 Laravel Passport 中获取刷新令牌?

Laravel - 加载常见的页眉和页脚以查看

使用 Laravel 和 Passport 验证失败时响应状态码 401?

在 laravel 查询构建器中使用多个 where 子句

如何使用 laravel eloquent 获得select count(*) group by

路由模型绑定不起作用

Laravel 5 - 在控制器文件中为多个路由定义中间件

Laravel 4 - 如何将所有字段的所有验证错误消息作为 JSON struct 返回?

日期验证 - 如何本地化/翻译字符串今天和明天

Laravel Mail 发送Electron邮件但返回 false

Laravel 5 - 判断日期是否是今天

Laravel 更新查询

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈:

Laravel Mix:更新 Node.js 依赖项

Laravel - Union + 分页?

在 Laravel 4 中向表单添加类/ID