How to order by using appended attribute in Laravel

后端 未结 5 1042
南旧
南旧 2021-02-13 19:05

I created an appends attribute in Laravel Model, from the code below.

    protected $appends = array(\'total\'=>\'\');

And I set the returne

5条回答
  •  醉话见心
    2021-02-13 19:52

    the orderBy takes an actual database field not an appended one

    try this

    $products = Product::all();
    $products = $products->sortBy(function($product){
        return $product->total;
    });
    

提交回复
热议问题