laravel - Laravel5.5 Eloquent Relations with() 无效
问题描述
我有 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();
这是正确的,没有任何错误,但结果不排序。
请帮我看看我错在哪里?
非常感谢。
解决方案
推荐阅读
- php - 表单提交点击保存按钮后,我想重定向到它自己的带有 id 的编辑页面
- php - 如何在 Pages Wordpress 中编辑代码文件模板自定义页面
- python - Python:绘制负(x,y)坐标的频率
- python - 设置一个默认的全局pycharm项目
- html - css !important 不是插件的覆盖样式
- tensorflow - 从 Tensorflow Mixed Precision Tutorial 开始使用 TensorCore
- android - 如何在项目中使用不同版本的 Okhttp?
- java - WebClient (spring-boot-starter-webflux) 向错误的 url 发送请求
- ios - Implementing TableView in ScrollView doesn't show the table
- sql - 查找列的最大值,按不同表中的列分组