用户 = 带有成员的模型。 UserPreferences = 保存成员所有其他信息的模型。 类别 = 类别
在 UserPreferences 模型中,我将成员希望出现在“positions_desired_to_work”列中的类别保存为 json。 [“1”、“2”、“7”]
我想做的是:
示例:$ category-> id(id = 1 个游戏类别)
我在页面视图中进入游戏类别,/类别/游戏
我想在 UserPreferences 模型的“positions_desired_to_work”列的数组中引入哪些成员存储了游戏类别。
有一个这样的查询,但它返回空白。
$caregivrs = DB::table('users')
->whereExists(function ($query) use ($categoryId ) {
$query
->select('*')
->from('user_preferences')
->whereColumn('user_preferences.user_id', 'users.id')
->whereJsonContains('positions_desired_to_work' , "{$categoryId}");
})
->dd();
输出:
"select * from `users` where exists (select * from `user_preferences` where `user_preferences`.`user_id` = `users`.`id` and json_contains(`positions_desired_to_work`, ?))