我正在执行 Laravel 任务,我想对与另一个表相关的表进行排序。我在这里阅读了文档: https: //laravel.com/docs/5.4/eloquent-relationships#counting-lated-models 我的代码:
$addonCategories = AddonCategory::withCount('addons')->sortable()->orderBy('id', 'DESC')->with('addons')->paginate(20);
return view('admin.addonCategories', array(
'addonCategories' => $addonCategories,
));
插件类别控制器:
class AddonCategory extends Model
{
use Sortable;
protected $casts = ['addons_count' => 'integer'];
public $sortable = ['name', 'type', 'addons_count', 'created_at'];
public function items()
{
return $this->belongsToMany(Item::class);
}
public function addons()
{
return $this->hasMany('App\Addon');
}
}
addonCategories.blade.php:
<th>@sortablelink('addons_count', __('storeDashboard.acpTableNOA'))</th>
当我使用 sortable 时,出现错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'addons_count' in 'order clause'
我认为这是因为数据库表中没有addons_count。那么如何像这样使用 {relations}_count 进行排序呢?
幕布斯6054654