source

Laravel의 ORM을 사용하여 결과를 "제한"할 수 있는 방법이 있습니까?

bestscript 2022. 12. 24. 20:36

Laravel의 ORM을 사용하여 결과를 "제한"할 수 있는 방법이 있습니까?

Laravel의 ORM을 사용하여 결과를 "제한"할 수 있는 방법이 있습니까?

 SELECT * FROM  `games` LIMIT 30 , 30 

웅변가라면?

웅변을 확장한 게임 모델을 만들고 다음을 사용합니다.

Game::take(30)->skip(30)->get();

take()여기서 30개의 레코드가 나오고skip()여기서 30개의 레코드로 오프셋됩니다.


최신 Larabel 버전에서는 다음 기능도 사용할 수 있습니다.

Game::limit(30)->offset(30)->get();

결과에 페이지 매기기를 원하는 경우 통합 페이지 매기기기를 사용하면 매우 효과적입니다.

$games = Game::paginate(30);
// $games->results = the 30 you asked for
// $games->links() = the links to next, previous, etc pages

LIMIT를 다음과 같이 사용할 수 있습니다.

Model::take(20)->get();

또한 다음과 같이 사용할 수 있습니다.

1등만 하려면

 $cat_details = DB::table('an_category')->where('slug', 'people')->first();

한계 및 간격띄우기로 얻는 방법

$top_articles = DB::table('an_pages')->where('status',1)->limit(30)->offset(0)->orderBy('id', 'DESC')->get();
$remaining_articles = DB::table('an_pages')->where('status',1)->limit(30)->offset(30)->orderBy('id', 'DESC')->get();

언급URL : https://stackoverflow.com/questions/15229303/is-there-a-way-to-limit-the-result-with-eloquent-orm-of-laravel