2017-08-29 52 views
0

我需要一個「秩序」列訂購如何訂購使用具有()

Content::with('blocks.settings')->where('slug', $slug)->first(); 

塊,我該怎麼辦那渴望的關係?

我是知道這種方法的:

with(array('model' => function($query) { 
     $query->orderBy('result', 'DESC'); 
    })) 

,但我不知道怎麼會在我的情況下工作嗎?我正在使用嵌套的急切加載,在我看來,上述方法只適用於單層急切加載?

有人可以給我一個想法如何解決這個問題嗎?

回答

1

要在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(); 
2

這應該解決您的問題:

Content::with(array(
    'blocks' => function($query) { 
     $query->orderBy('order', 'DESC'); 
    }, 
    'blocks.settings' 
))->where('slug', $slug)->first();; 

首先,我們告訴它由order列裝載塊,命令他們。數組中的第二個元素也告訴用所有塊加載設置。