I have the following db structure:
items:
id, name, user_id
users table:
id, name
user_favorites table:
id, user_id, item_id
在我的items permalink页面上,我有一个"Add to favorites"(添加到Collection 夹)按钮,可以在user_favorites
行中插入新行
如果用户的Collection 夹中已经有"从Collection 夹中删除"按钮,我希望能够将其替换为"从Collection 夹中删除"按钮.
我无法理解这背后的逻辑——我是否需要判断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
}
}