我如何使用 with() 订购热切的关系
how do I order eager relationship using with()
我需要订购
Content::with('blocks.settings')->where('slug', $slug)->first();
被 'order' 栏挡住,我该怎么做?
我知道这种方法:
with(array('model' => function($query) {
$query->orderBy('result', 'DESC');
}))
但我不确定这对我的情况有何影响?我正在使用嵌套的预加载,在我看来,上述方法仅适用于单级预加载?
谁能告诉我如何解决这个问题?
这应该可以解决您的问题:
Content::with(array(
'blocks' => function($query) {
$query->orderBy('order', 'DESC');
},
'blocks.settings'
))->where('slug', $slug)->first();;
首先,我们告诉它加载块并按 order
列对它们进行排序。数组中的第二个元素也告诉加载所有块的设置。
要在 blocks
模型上添加约束,
Content::with(array(
'blocks.settings' => function($query) {
$query->orderBy('order', 'DESC'); // this constraint works on blocks model
}
))->where('slug', $slug)->first();
如果你想在 settings
模型上添加约束,试试这个
Content::with(array(
'blocks'=> function($query) {
$query->with(array(
'settings'=> function($query) {
$query->orderBy('order', 'DESC'); // this constraint works on blocks model
}
))
->ordery('order', 'DESC'); // this constraint works on blocks model
}
))->where('slug', $slug)->first();
我需要订购
Content::with('blocks.settings')->where('slug', $slug)->first();
被 'order' 栏挡住,我该怎么做?
我知道这种方法:
with(array('model' => function($query) {
$query->orderBy('result', 'DESC');
}))
但我不确定这对我的情况有何影响?我正在使用嵌套的预加载,在我看来,上述方法仅适用于单级预加载?
谁能告诉我如何解决这个问题?
这应该可以解决您的问题:
Content::with(array(
'blocks' => function($query) {
$query->orderBy('order', 'DESC');
},
'blocks.settings'
))->where('slug', $slug)->first();;
首先,我们告诉它加载块并按 order
列对它们进行排序。数组中的第二个元素也告诉加载所有块的设置。
要在 blocks
模型上添加约束,
Content::with(array(
'blocks.settings' => function($query) {
$query->orderBy('order', 'DESC'); // this constraint works on blocks model
}
))->where('slug', $slug)->first();
如果你想在 settings
模型上添加约束,试试这个
Content::with(array(
'blocks'=> function($query) {
$query->with(array(
'settings'=> function($query) {
$query->orderBy('order', 'DESC'); // this constraint works on blocks model
}
))
->ordery('order', 'DESC'); // this constraint works on blocks model
}
))->where('slug', $slug)->first();