Tha*_*anh 1 eloquent laravel-5
我有一个类别名称 A,路径为:1-2-
一些孩子的路径如下:
乙:1-2-3-
C:1-2-4-
D:1-2-5-
我想将类别 A 从 1 移动到 6,路径为:6-2-
如何将类别 B、C、D 更新为 6-2-%
我找到了这段 php 代码:
更新类别 SET path = REPLACE(path, '1-2-', '6-2-') WHERE path LIKE '1-2-%'
但我不知道如何将它用于 laravel。
小智 5
你可以这样做:
DB::table("category")
->where("path", "LIKE", "1-2-%")
->update(["path" => DB::raw("REPLACE(path, '1-2-', '6-2-')")]);
Run Code Online (Sandbox Code Playgroud)
或者,如果您有类别表模型:
Category::query()
->where("path", "LIKE", "1-2-%")
->update(["path" => DB::raw("REPLACE(path, '1-2-', '6-2-')")]);
Run Code Online (Sandbox Code Playgroud)