【发布时间】:2014-10-02 06:37:28
【问题描述】:
我有以下数据库结构:
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
}
}
【问题讨论】:
标签: php mysql sql laravel eloquent