多对多关系OrderBy-Laravel查询生成器 [英] many-to-many relationship OrderBy - Laravel query builder

查看:52
本文介绍了多对多关系OrderBy-Laravel查询生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按价格表中的价格订购产品该价格可以是多种货币,因为我添加了按以下代码计算的 dolor_price .但结果是以下错误
SQLSTATE [42S22]:找不到列:1054 未知列订单子句"中的"options.dolor_price"

I am trying to order products by thp price in options table the price can be in multi currency for that i add dolor_price calculated as code below. but the result was the following Error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'options.dolor_price' in 'order clause'

    $products=Product::with(["options"=> function($option){
        $option->where('name' ,'unique name' );

        $option->selectSub(function ($q) {
            $rateEruToDolor =2;
            $rateAedToDolor =3;
            $q->selectRaw(' IF(currency=0,price * ?, IF(currency=1,price * ?, price))',
            [$rateEruToDolor,$rateAedToDolor]);
        }, 'dolor_price');
    }]);
    $products->orderBy('options.dolor_price');
    dd($products->get()->toArray());

推荐答案

尝试一下:

$rateEruToDolor =2;
$rateAedToDolor =3;
$products = Product::join('product_options', 'products.id', '=', 'product_options.product_id')
      ->join('options', 'product_options.option_id', '=', 'options.id')
      ->selectRaw('price, currency, IF(currency=0, price * ?, IF(currency=1, price * ?, price)) 
                   as dolor_price', [$rateEruToDolor, $rateAedToDolor]);
$products->orderBy('dolor_price');
dd($products->get()->toArray());

列出产品/选项表中的所有字段,以及 selectRaw('xxx,xxx,价格,货币,,IF ...)方法.希望这会有所帮助

List all of the fields in your products/options table along with price and currency already listed in the selectRaw('xxx, xxx, price, currency, IF ...) method above. Hope this helps

这篇关于多对多关系OrderBy-Laravel查询生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆