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