我有以下数据库结构:
items:
id, name, user_id
users table:
id, name
user_favorites table:
id, user_id, item_id
在我的项目永久链接页面上,我有一个“添加到收藏夹”按钮,可以将新行插入到user_favorites
如果用户已将其添加到收藏夹中,我希望能够将其替换为“从收藏夹中删除”按钮。
我无法弄清楚这背后的逻辑 - 我是否需要检查行是否存在于user_favorites
有当前用户的 id 和永久链接项目 id 吗?这对我不起作用:
if (Auth::user()->id) {
if (!is_null(DB::table('user_favorites')->where('user_id', '=', Auth::user()->id)->where('item_id', '=', $item->id)->first())) {
// remove from favorites button will show
}
}
你可能想要这样的东西:
$user_favorites = DB::table('user_favorites')
->where('user_id', '=', Auth::user()->id)
->where('item_id', '=', $item->id)
->first();
if (is_null($user_favorites)) {
// It does not exist - add to favorites button will show
} else {
// It exists - remove from favorites button will show
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)