我有 3 个模型,并且我已经设置了关系。
A型:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ComponentA extends Model
{
protected $table = 'ComponentA';
protected $primaryKey = 'ComponentAId';
public function ComponentB() {
return $this->hasOne(
'App\Model\ComponentB', 'ComponentBId', 'ComponentBId'
);
}
B型:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ComponentB extends Model
{
protected $table = 'ComponentB';
protected $primaryKey = 'ComponentBId';
public function ComponentC() {
return $this->hasOne(
'App\Model\ComponentC', 'ComponentCId', 'ComponentCId'
)->withDefault();
}
}
C型:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class ComponentC extends Model
{
protected $table = 'ComponentC';
protected $primaryKey = 'ComponentCId';
}
现在,在控制器中,我想从 A 调用 C并按C 列排序,我使用with()方法。
控制器:
$this->ComponentA->with(['ComponentB.ComponentC' => function($query) {
$query->orderby('ComponentCId','DESC');
}])->paginate();
这是正确的,没有任何错误,但结果不排序。
请帮我看看我错在哪里?
非常感谢。