php 检查行是否存在,Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25209302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:43:56  来源:igfitidea点击:

Check if row exists, Laravel

phpmysqlsqllaraveleloquent

提问by O P

I have the following db structure:

我有以下数据库结构:

items:
id, name, user_id

users table:
id, name

user_favorites table:
id, user_id, item_id

On my items permalink pages, I have an 'Add to favorites' button which inserts a new row into user_favorites

在我的项目永久链接页面上,我有一个“添加到收藏夹”按钮,可将新行插入到 user_favorites

I want to be able to replace it for a 'Remove from favorites' button if the user already has it in their favorites.

如果用户已经在他们的收藏夹中拥有它,我希望能够将其替换为“从收藏夹中删除”按钮。

I can't figure out the logic behind this - do I need to check if a row exists in user_favoritesthat has the current user's id and the permalink item id? This did not work for me:

我无法弄清楚这背后的逻辑 - 我是否需要检查是否存在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
    }
}

回答by Simone

You may want something like this:

你可能想要这样的东西:

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

回答by lijinma

I advise you to use exists()or count()to check, not use first().

我建议您使用exists()count()检查,而不是使用first().

The fastest way:

最快的方法:

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

Or:

或者:

$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

The faster way: only select id

更快的方法:只选择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

The normal way:

正常方式:

$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

回答by Ajay Kumar Ganesh

Let User_favoritebe a model that accesses your user_favoritestable

让我们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();
}

回答by Amit Gupta

The simplest way to do is to use toggle()method of many-to-many relationship.

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

e.g.

例如

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

The many-to-many relationship also provides a toggle method which "toggles" the attachment status of the given IDs. If the given ID is currently attached, it will be detached. Likewise, if it is currently detached, it will be attached

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

It also returns an array which tells you if the IDis attached or detached in DB.

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