0

I'm creating a wishlist where the user can save his own products and save them. What I want is that a user can't save a product more than one time. So, what I'm doing is, if the product_id exists in the DB, I don't want to save the new record.

This is my code, what am I doing wrong?

public function addWishlist(Wishlist $wishlist) {        
    $wishlist->user_id = request('user_id');
    $wishlist->product_id = request('product_id');

    if(DB::table('wishlists')->where('product_id' !== $wishlist->product_id)) {
        $wishlist->save();
        return redirect()->back();
    }
}
Daniel Hernandez
  • 195
  • 6
  • 17
  • There's already an eloquent function for that `updateOrCreate`. https://laravel.com/docs/5.6/eloquent – hungrykoala Jun 18 '18 at 05:49
  • use firstOrCreate and then check for wasRecentlyCreated https://stackoverflow.com/questions/18498518/how-to-check-if-a-record-is-new-in-laravel – Payam Khaninejad Jun 18 '18 at 07:31
  • `where('product_id' !== $wishlist->product_id)` -- what do you expect from this conditon? It will select all items from `wishlists`with the condition `true` – Nico Haase Jun 19 '18 at 07:26

4 Answers4

2

what about this

public function addWishlist(Wishlist $wishlist) {  
    $userId = request('user_id');
    $productId = request('product_id');

    $found = Wishlist::where('user_id', $userId)->where('product_id', $productId)->count();

    if($found == 0) {
        $wishlist->user_id = $userId;
        $wishlist->product_id = $productId;
        $wishlist->save();
        return redirect()->back();
    }
}
rkj
  • 8,067
  • 2
  • 27
  • 33
0
public function addWishlist(Wishlist $wishlist) {   

    if(DB::table('wishlists')->where(['user_id'=>$user_id,'product_id'=>$wishlist->product_id])->exists()){
         // record already exist 
         return redirect()->back();
    }else{
         // record not exist , save the record
         $withlist = new Wishlist();     
         $wishlist->user_id = request('user_id');
         $wishlist->product_id = request('product_id');
         $wishlist->save();
         return redirect()->back();
    }
}
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
0

You can use firstOrNew and you need to check with user_id and product_id combination

    $wishlist::firstOrNew(array('user_id' => request('user_id'),'product_id'=>request('product_id')));
    $wishlist->user_id = request('user_id');
    $wishlist->product_id = request('product_id');
    $wishlist->save();
    return redirect()->back();
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • It isn't working correctly. It's saving the record if the product_id is repeated. So, I'm having the same problem – Daniel Hernandez Jun 18 '18 at 05:57
  • Did you noticed this line `you need to check with user_id and product_id combination` ? @danhergir – JYoThI Jun 18 '18 at 05:59
  • I only need to check the product_id. I changed your code, but it's not working anyway – Daniel Hernandez Jun 18 '18 at 06:07
  • if your not check with user id means .if A user choose one product means . B user can't choose that product . is it okay ? @danhergir – JYoThI Jun 18 '18 at 06:11
  • No, what I mean is if a user added that product to the wishlist, that user can't do it again. – Daniel Hernandez Jun 18 '18 at 06:17
  • I can understood your point . if `user A` added `product_id` 1 .Now `user B` trying to add that the same `product_id` 1 . now you can't insert because that `product_id` 1 already exists in table that's why i'm telling you need to check with combination column `user_id` and `product_id` – JYoThI Jun 18 '18 at 06:22
  • use firstOrCreate and then check for wasRecentlyCreated https://stackoverflow.com/questions/18498518/how-to-check-if-a-record-is-new-in-laravel – Payam Khaninejad Jun 18 '18 at 07:30
0

Have you tried firstOrCreate()

 $user = User::firstOrCreate(['email' => $email])
Faizan Fayaz
  • 540
  • 5
  • 16