Laravel字符串到Int.数组



我使用的是laravel 7。

我有一个像这样雄辩的回报:

$menu_roles_parent = MenuRoles::where('role_id',1)->pluck('pai_id');

这给我回了一条这样的绳子:["15,19"]。问题是,我想在下一个雄辩的查询中使用这个返回,例如:

$menus = AppMenu::whereIn('id',$menu_roles_parent)->with('submenus')->where('active',1)->orderBy('position')->get();

查询运行,但我想在下面更改:

select * from 'menus' where 'id' in ('15,19') and 'active' = 1 order by 'position' asc

select * from 'menus' where 'id' in (15,19) and 'active' = 1 order by 'position' asc

我在想把字符串返回放在一个整数数组中,有人能帮我吗?谢谢

您可以使用explode原生php函数将字符串分解为由分隔符分隔的数组。

$menu_roles_parent = explode(",", $menu_roles_parent[0]);

如果强制转换列,它将使用Eloquentpluck()自动强制转换。像这样向模型中添加强制转换应该可以解决您的问题。

class MenuRoles {
protected $casts = [
'role_id' => 'int',
];
}

最新更新