我试图将我的laravel原始查询更改为雄辩,因为现在我已经掌握了一些雄辩的基础知识,并且我尝试在laravel中制作博客。 但是一些复杂性仍然保持不变。
首先:这是我的Raw SQL:
select group_concat(DISTINCT q.sku SEPARATOR ", ") as sku, `sales_flat_order`.`status`, `sales_flat_order`.`increment_id`, `sales_flat_order`.`shipping_description`, `sales_flat_order`.`subtotal`, `sales_flat_order`.`customer_email`, `d`.`country_id`, `d`.`region`, `d`.`city`, `d`.`postcode`, group_concat(DISTINCT q.name SEPARATOR ", ") as name, concat(sales_flat_order.created_at) AS created_at from `sales_flat_order` left join `sales_flat_order_item` as `q` on `sales_flat_order`.`entity_id` = `q`.`order_id` left join `sales_flat_order_address` as `d` on `d`.`parent_id` = `sales_flat_order`.`entity_id` group by `sales_flat_order`.`increment_id` order by `sales_flat_order`.`increment_id` asc
我在Laravel的Raw查询:
SalesFlatOrder::leftJoin('sales_flat_order_item as q','sales_flat_order.entity_id', '=','q.order_id')
->leftJoin('sales_flat_order_address as d', 'd.parent_id', '=', 'sales_flat_order.entity_id')
->select((array(DB::Raw('group_concat(DISTINCT q.sku SEPARATOR ", ") as sku'),'sales_flat_order.status','sales_flat_order.increment_id', 'sales_flat_order.shipping_description','sales_flat_order.subtotal','sales_flat_order.customer_email','d.country_id', 'd.region', 'd.city','d.postcode',DB::raw('group_concat(DISTINCT q.name SEPARATOR ", ") as name'),DB::raw('concat(sales_flat_order.created_at) AS created_at'))))
->groupBy('sales_flat_order.increment_id')
->orderBy('sales_flat_order.increment_id')
->paginate(10);
现在我正在尝试将整个原始查询更改为雄辩。我已经制作了模特。以下是我在我的控制器中的雄辩查询。
public function detailed(){
$sales = SalesFlatOrder::with('address')->groupBy('increment_id')->orderBy('increment_id')->paginate(10);
return View::make('detailed')->with('sales', $sales);
}
我的问题:1: 我确实有小组concat(Distinct q.sku)。那么如何以雄辩的方式做到这一点。因为有时你需要按日期分组。或按订单分组。那么如何显示不同的数据所以sku列。那么如何将这个完整的fledge原始查询转换为Eloqeunt,其中我们有group_concat,区分了很多DB的列
答案 0 :(得分:1)
首先创建一个自定义集合:
$customCollection = new \Illuminate\Database\Eloquent\Collection;
然后,您可以这样做:
$user =new User;
$user->setRawAttributes((array) $userRawData);
$customCollection->add($user);
问候!
答案 1 :(得分:0)
您可以使用模型关系,雄辩的数据库表命名约定(不在模型中指定表名),路由数据绑定crom数据库和范围,从RAW转换为Eloquent优雅查询。
但是,有时你需要帮助自己使用一些RAW进行更复杂的操作。