我在Laravel Model中创建了一个appends属性,来自下面的代码.
protected $appends = array('total'=>'');
我设置了返回的值.
public function getTotalAttribute(){ return ProductPart::where('product_id',$this->id)->count(); }
然后我想通过使用total
属性从数据库中订购记录
我尝试使用,Product::orderBy('total','desc')->get()
但它没有用.
有没有人对此提出一些建议?
orderBy采用实际的数据库字段而不是附加的数据库字段
试试这个
$products = Product::all(); $products = $products->sortBy(function($product){ return $product->total; });