Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅向用户显示他没有的产品';t使用laravel雄辩地购买_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 仅向用户显示他没有的产品';t使用laravel雄辩地购买

Php 仅向用户显示他没有的产品';t使用laravel雄辩地购买,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,是否有任何直接查询,我们可以向用户显示登录用户尚未使用laravel雄辩功能购买的产品 我不熟悉拉威尔关系和雄辩的问题 到目前为止,我已经尝试使用如下查询 DB::raw('Select * from products where products.id not in ( "Select product_id from order_product left join orders on orders.id = order_product.order_id left join use

是否有任何直接查询,我们可以向用户显示登录用户尚未使用laravel雄辩功能购买的产品

我不熟悉拉威尔关系和雄辩的问题

到目前为止,我已经尝试使用如下查询

DB::raw('Select * from products 
where products.id not in ( "Select product_id from order_product
left join orders on orders.id = order_product.order_id 
left join users on users.id = orders.user_id 
where users.id='.$user_id.'")');

您可以尝试这样的方法,它会解决您的问题

$user_id = auth()->user()->id;

Product::where('status', 'active')

  ->whereNotIn('id', function($query) use ($user_id) {

    $query->select('product_id')->from((new OrderProduct)->getTable())

      ->where('user_id', $user_id)->where('status', 'delivered')

        ->pluck('product_id')->toArray();

  });

欢迎来到苏。。。到目前为止你试过什么?你猜怎么办?伪代码:
Product::whereDoesntHave('user',function($query){$query->where('id',Auth::id();})我不熟悉laravel雄辩的我被这个问题困住了,但找不到我通常用原始查询尝试的简单答案有没有更好的解决方案。在某些方面是新的没关系:)。。。如果使用原始查询,您将如何做到这一点?您是否在这些表的模型上设置了关系?是的,我在模型中设置了关系我有一个产品和用户模型,但找到了最佳解决方案。当您可以创建一个关系并使用诸如
whereHas()
whereDoesntHave()之类的关系查询时,为什么要将其变得如此复杂
?好的,谢谢你的建议,我肯定会继续研究这一部分,以进一步回答拉雷维尔雄辩的问题:)