在原始mysql中编写有说服力的查询

在原始mysql中编写有说服力的查询,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,我想知道如何将这两个有说服力的查询写入原始SQL?还有一种方法可以将雄辩的序列转换成原始的SQL序列,以便我可以使用它们吗 public function items() { return $this->belongsToMany(Product::class, 'order_items', 'order_id', 'product_id')->withPivot('quantity', 'price'); } public func

我想知道如何将这两个有说服力的查询写入原始SQL?还有一种方法可以将雄辩的序列转换成原始的SQL序列,以便我可以使用它们吗

    public function items()
    {
        return $this->belongsToMany(Product::class, 'order_items', 'order_id', 'product_id')->withPivot('quantity', 'price');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

如果我们讨论items方法中使用的第一个查询。将其更改为原始SQL后,它将成为

DB::select('select `products`.*, `order_items`.`order_id` as `pivot_order_id`, `order_items`.`product_id` as `pivot_product_id`, `order_items`.`quantity` as `pivot_quantity`, `order_items`.`price` as `pivot_price` from `products` inner join `order_items` on `products`.`id` = `order_items`.`product_id` where `order_items`.`order_id` = `order_id`');
DB::select('select * from `users` where `users`.`id` = `id`');
用户方法中的第二个查询将变为

DB::select('select `products`.*, `order_items`.`order_id` as `pivot_order_id`, `order_items`.`product_id` as `pivot_product_id`, `order_items`.`quantity` as `pivot_quantity`, `order_items`.`price` as `pivot_price` from `products` inner join `order_items` on `products`.`id` = `order_items`.`product_id` where `order_items`.`order_id` = `order_id`');
DB::select('select * from `users` where `users`.`id` = `id`');

是的,这些查询非常有效,非常感谢。这将帮助我更好地理解它。