【问题标题】:Check if row exists, Laravel检查行是否存在,Laravel
【发布时间】: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


    【解决方案1】:

    你可能想要这样的东西:

    $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
    }
    

    【讨论】:

    • 不应该 if (is_null($user)) { 实际上是 if (is_null($user_favorites)) {
    • 是的,打错了,已修复:)
    • 这是原始方法。使用模型更好,获取 Auth::getUser()->id 性能更快。
    • 当您获得$user_favorites 的信息时,如何检查特定列是否为空?例如$user_favorites->date
    • 有趣的是,如果你不放 if 和 else 那么你会尝试获取非对象的属性,而不是返回 null(我来自 java)
    【解决方案2】:

    我建议你使用exists()count()来检查,不要使用first()

    最快的方法:

    $result = DB::table('user_favorites')
        ->where('user_id', '=', Auth::user()->id)
        ->where('item_id', '=', $item->id)
        ->exists();
    

    或者:

    $result = DB::table('user_favorites')
        ->where('user_id', '=', Auth::user()->id)
        ->where('item_id', '=', $item->id)
        ->count();
    

    SQL:

    select count(*) as aggregate from `user_favorites` where *** limit 1
    

    更快的方法:只选择id

    $result = DB::table('user_favorites')
        ->where('user_id', '=', Auth::user()->id)
        ->where('item_id', '=', $item->id)
        ->first(['id']);
    

    SQL:

    select id from `user_favorites` where *** limit 1
    

    正常方式:

    $result = DB::table('user_favorites')
        ->where('user_id', '=', Auth::user()->id)
        ->where('item_id', '=', $item->id)
        ->first();
    

    SQL:

    select * from `user_favorites` where *** limit 1
    

    【讨论】:

    • ->exists() 对我不起作用,但 ->count() 起作用。 (使用 mysql,L 5.2)。
    【解决方案3】:

    User_favorite 成为访问您的user_favorites 表的模型

    $result = User_favorite::where('user_id',Auth::getUser()->id)
                             ->where('item_id',$item->id)
                             ->first();
    
    if (is_null($result)) {
    // Not favorited - add new
        User_favorite::create(['user_id'=>Auth::getUser()->id,'item_id'=>$item->id]);
    } else {
    // Already favorited - delete the existing
        $result->delete();
    }
    

    【讨论】:

    • 这个比较有说服力
    • 如何对多个数据库执行此操作,例如我将在哪里选择和使用连接方法我如何找到是否存在 $results = DB::connection("siteDB")->[ 存在哪里 id = ? ??? ]
    【解决方案4】:

    最简单的方法是使用toggle()的多对多关系方法。

    例如

    $user->roles()->toggle([1, 2, 3]);
    

    多对多关系还提供了一种切换方法 “切换”给定 ID 的附件状态。如果给定的 ID 是 当前已连接,它将被分离。同样,如果当前是 分离了,会附加的

    它还返回一个数组,告诉您 ID 在 DB 中是附加还是分离。

    【讨论】:

      猜你喜欢
      • 2018-02-28
      • 2015-01-21
      • 2020-06-21
      • 2017-07-13
      • 2018-04-04
      • 2015-07-19
      • 2018-09-22
      相关资源
      最近更新 更多