답변:
쿼리 함수로 관계를 확장 할 수 있습니다.
<?php
public function comments()
{
return $this->hasMany('Comment')->orderBy('column');
}
[댓글 후 편집]
<?php
class User
{
public function comments()
{
return $this->hasMany('Comment');
}
}
class Controller
{
public function index()
{
$column = Input::get('orderBy', 'defaultColumn');
$comments = User::find(1)->comments()->orderBy($column)->get();
// use $comments in the template
}
}
기본 사용자 모델 + 간단한 컨트롤러 예; 주석 목록을 가져올 때 Input :: get ()을 기반으로 orderBy ()를 적용하십시오. (일부 입력 확인을 수행하십시오;))
나는 또한 당신이 할 수 있다고 믿습니다 :
$sortDirection = 'desc';
$user->with(['comments' => function ($query) use ($sortDirection) {
$query->orderBy('column', $sortDirection);
}]);
이를 통해 관련된 각 주석 레코드에서 임의의 논리를 실행할 수 있습니다. 거기에 다음과 같은 물건을 넣을 수 있습니다.
$query->where('timestamp', '<', $someTime)->orderBy('timestamp', $sortDirection);
appointments및 schedules)이 있고 쿼리는 간단합니다. get appointmentsorder by schedules. datetime내림차순. 나는 테이블에 새 열을 추가하여 솔루션이 appointments가게에 datetime테이블을 schedules. 이제 주문 만하면됩니다 appointments. datetime나는 그것이 최선의 방법이 아니라는 것을 알고 있지만 문제를 해결합니다. XD