[英]Wishlist system with Laravel and Spatie query builder
I need to create a wishlist with laravel, I already use spatie/query-builder to get products.我需要使用 laravel 创建一个愿望清单,我已经使用 spatie/query-builder 来获取产品。 At this time everything works fine and I can retrieve products already added to wishlist.此时一切正常,我可以检索已添加到愿望清单的产品。 I need help for reverse the process.我需要帮助来扭转这个过程。
Three tables are used:使用了三个表:
If record exists in pivot table product is in wishlist如果 pivot 表中存在记录 产品在愿望清单中
So with this custom query builder filter:因此,使用此自定义查询构建器过滤器:
return $query->join('product_favorites', function ($query) {
$query->on('product_favorites.product_id', '=', 'products.id');
})->where('product_favorites.user_id', '=', Auth::id());
I can get products who are in current user wishlist.我可以获得当前用户愿望清单中的产品。 I want to reverse the process.我想扭转这个过程。 Get all products who aren't in product_favorites table.获取所有不在product_favorites表中的产品。
How do you think I can achieve this?你觉得我怎么能做到这一点?
Thank's for your help谢谢你的帮助
I'm not sure but this on seems to work:我不确定,但这似乎有效:
return $query->leftJoin('product_favorites', function ($query) {
$query->on('product_favorites.product_id', '=', 'products.id');
})->where('product_favorites.user_id', '=', null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.