IT story

Laravel 4 : Eloquent ORM을 사용하여 "주문"하는 방법

hot-time 2020. 5. 25. 08:09
반응형

Laravel 4 : Eloquent ORM을 사용하여 "주문"하는 방법


이 질문에는 이미 답변이 있습니다.

간단한 질문-Laravel 4에서 내림차순으로 'id'로 어떻게 주문합니까?

내 컨트롤러의 관련 부분은 다음과 같습니다.

$posts = $this->post->all()

이해 하듯 이이 줄을 사용하십시오.

->orderBy('id', 'DESC');

그러나 위의 코드와 어떻게 일치합니까?


게시물을 모델로 사용하는 경우 (종속성 주입없이) 다음을 수행 할 수도 있습니다.

$posts = Post::orderBy('id', 'DESC')->get();

Eloquent ORM을 사용하는 경우 범위 사용을 고려해야합니다. 그러면 논리가 속한 모델에서 논리가 유지됩니다.

따라서 모델에는 다음이 있습니다.

public function scopeIdDescending($query)
{
        return $query->orderBy('id','DESC');
}   

그리고 모델 밖에서 당신은 가질 것입니다 :

$posts = Post::idDescending()->get();

자세한 정보 : http://laravel.com/docs/eloquent#query-scopes


이것이 내가 갈 방법입니다.

$posts = $this->post->orderBy('id', 'DESC')->get();

참고 URL : https://stackoverflow.com/questions/17553181/laravel-4-how-to-order-by-using-eloquent-orm

반응형