如何用最有说服力的ORM进行三维查询?有“采购商”、“产品”、“订单”、“采购”四个表和“采购商”、“产品”、“订单”、“采购”四个模型。下面的表结构
采购员表
id --- name --- email
1 ---- John --- john@example.com
2 ---- Kelly -- kelly@example.com
3 ---- Chery -- chery@example.com
产品表:
id --- name ---- quantity ---- price
1 ---- Apple --- 5Kg --------- 10$
2 ---- Orange -- 10kg -------- 11$
3 ---- Grapes -- 20kg -------- 15$
订单表:
id --- buyer_id --- status
1 ---- 1 ----------- 0
2 ---- 2 ----------- 1
3 ---- 3 ----------- 0
采购表:
id --- order_id ---- product_id --- price --- quantity --- total_price
1 ---- 1 ----------- 2 ------------ 11$ ----- 2Kg -------- 22$
2 ---- 1 ----------- 1 ------------ 10$ ----- 1Kg -------- 10$
3 ---- 1 ----------- 3 ------------ 15$ ----- 5Kg -------- 75$
4 ---- 2 ----------- 1 ------------ 10$ ----- 3kg -------- 30$
5 ---- 2 ----------- 2 ------------ 11$ ----- 4kg -------- 44$
6 ---- 2 ----------- 3 ------------ 15$ ----- 3kg -------- 45$
现在我想用产品名称查询所有订单,而不是查询构建器。
$orders = Order::where('status', 0)->...........??
请帮帮我,我怎么才能做到这一点呢?这是可能的或不可能的。如果可能的话,请帮帮我。
发布于 2020-04-12 18:14:16
现在,我想要按laravel eloquent查询所有订单
你能更具体地说明你到底需要什么吗?
在您的示例中,您已经在查询orders,并且如果您已经在模型中定义了关系,那么您已经可以访问相关的表。使用eloquent,可以使用->whereHas
see Laravel doc跨关系进行查询,可以使用->with
see Eager loading将所有相关信息传递给对象
我不确定这是否会有帮助!
发布于 2020-04-12 23:03:48
好吧,我不能在我的本地环境中做同样的事情,但我会给你一个基本的想法,如何获得每个相关数据的细节。确认您的模型具有指定的方法
Buyer.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;
class Buyer extends Model
{
/**
* other functions and properties.
*/
/**
* Get the orders associated with the buyer.
*/
public function orders()
{
return $this->hasMany('App\Order', 'buyer_id')->where('status', 1);
}
}
Order.php
class Order extends Model
{
/**
* other functions and properties.
*/
/**
* Get the purchases associated with the Order.
*/
public function purchases()
{
return $this->hasMany('App\Purchase', 'order_id');
}
/**
* Get the buyer belongs to the Order.
*/
public function buyer()
{
return $this->belongsTo('App\Buyer', 'buyer_id');
}
}
Purchase.php
class Purchases extends Model
{
/**
* other functions and properties.
*/
/**
* Get the user product belongs to the Purchase.
*/
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
/**
* Get the user product belongs to the Purchase.
*/
public function order()
{
return $this->belongsTo('App\Order', 'order_id');
}
}
Controller.php
$buyer_id = 1;
$orders = App\Buyer::where('id', $buyer_id)->with('orders')->get();
通过这种方式,您可以获得所有数据及其相关数据。也就是说,上面的查询将为您提供买家的详细信息和新的房产订单,这是一个数组,表示属于该特定买家的所有订单。
每个订单还具有属性购买,其中包括对象数组,并且每个对象(即购买对象)都具有表示产品详细信息的产品属性。
根据您的查询,如果您希望所有订单与买家和他们的购买详细信息(购买也包括产品详细信息)。
$orders = Order::where('status', 0)->with(['buyer', 'purchases'])->get();
如果你想要所有的订单与买家和他们的购买的产品细节。
$orders = Order::where('status', 0)->with(['buyer', 'purchases.product'])->get();
print_r($orders);
发布于 2020-04-13 02:11:57
这里(status = 0) =>未付款订单和(status = 1) =>已付款订单,并且始终只有一个买家有一个未付款订单,而不是更多。但有更多的付费订单。
Buyer.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;
class Buyer extends Model
{
/**
* other functions and properties.
*/
/**
* Get the orders associated with the buyer.
*/
public function orders()
{
return $this->hasMany('App\Order', 'buyer_id')->where('status', 0);
}
}
Order.php
class Order extends Model
{
/**
* other functions and properties.
*/
/**
* Get the purchases associated with the Order.
*/
public function purchases()
{
return $this->hasMany('App\Order', 'order_id');
}
}
Purchase.php
class Purchases extends Model
{
/**
* other functions and properties.
*/
/**
* Get the user product associated with the Purchase.
*/
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
}
现在就像这样查询。
$order = Order::with(['buyer', 'purchases', 'purchases.product'])->where('id', order_id)->first();
https://stackoverflow.com/questions/61169371
复制相似问题