dousui8263 2016-12-24 20:04
浏览 30
已采纳

Laravel复杂查询

I am new to Laravel and building a small Laravel 5.3 app offering free content files as well as files for purchase.

I want users to automatically have access to the free files (content which may be added periodically).

I have a products, purchases (pivot) and users table.

When a user is logged in, how can I query the products table like the following: select all free products (price=0) or join on purchases where users.user_id = purchases.user_id and products.id = purchases.product_id?

Any ideas, or is there a better way to accomplish the same thing?

Thanks

  • 写回答

2条回答 默认 最新

  • dss67853 2016-12-24 20:59
    关注

    How about if you use following query:

    $purchasedProducts = DB::table('purchases')
                ->join('products', 'products.id', '=', 'purchases.product_id')
                ->select('purchases.*', 'products.*')
                ->where([
                   ['purchases.user_id', '=', $loggedinUserId],
                   ['products.price', '=', 0],
                   ])
                ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部