Check if row exists, Laravel
Solution 1
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
}
Solution 2
I advise you to use exists()
or count()
to check, not use 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
$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
Solution 3
Let User_favorite
be a model that accesses your user_favorites
table
$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();
}
Solution 4
The simplest way to do is to use toggle()
method of many-to-many relationship.
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
It also returns an array which tells you if the ID
is attached or detached in DB.
Related videos on Youtube
O P
Updated on November 24, 2020Comments
-
O P over 3 years
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
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_favorites
that has the current user's id and the permalink item id? This did not work for me: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 } }
-
TunaMaxx over 9 yearsShouldn't
if (is_null($user)) {
actually beif (is_null($user_favorites)) {
-
Simone over 9 yearsYeah was a typo, fixed :)
-
Tendulraj almost 9 yearsthis one is more of eloquent approach
-
Ajay Kumar Ganesh over 8 yearsthis is raw method. Using model is better and getting Auth::getUser()->id is faster performance wise.
-
Pathros almost 8 yearsWhen you get that info
$user_favorites
, how do you check if a specific column is null or not? For example$user_favorites->date
-
P_95 over 7 yearsthat
->exists()
didn't work for me but ->count() worked. (using mysql, L 5.2). -
Datadimension over 6 yearshow to do this with multiple databases eg where I would select and use connection method how can I find if exists $results = DB::connection("siteDB")->[ exists where id = ???? ]
-
DragonFire almost 4 yearsinterestingly if you don't put the if and else then you get trying to get property of non object, instead of returning a null (i come from java)