Created
September 23, 2014 09:51
-
-
Save Ardakilic/630c26e047f200d5006b to your computer and use it in GitHub Desktop.
arda.pw blog yazımda hazırladığım Laravel Cache + Pagination kullanımı örnek kod.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Arda Kılıçdağı | |
* @link http://arda.pw | |
*/ | |
Route::get('cache_pagination', function(){ | |
//sayfa başına kaç tane olacak? | |
$sayfaBasinaKacTane = 10; | |
//sorgu çıktısını varsa cache'den alalım, yoksa Model'den çekip cache'e ekleyelim: | |
$query = Cache::remember('koleksiyonum', 15, function(){ | |
return Model::sorgum(); //kendinizce Fluent ya da Eloquent sorgu. | |
}); | |
//Koleksiyonda kaç tane element var? | |
$kacTaneVar = $query->count(); | |
//pagination classını initiate edelim | |
$pagination = Paginator::make($query->toArray(), $kacTaneVar, $sayfaBasinaKacTane); | |
//Koleksiyonu pagination değerine göre Slice'layalım: | |
//Slice'lanmış query, view'a bu gidecek: | |
$query = $query->slice( | |
($pagination->getCurrentPage()-1)*$pagination->getPerPage(), | |
$pagination->getPerPage() | |
); | |
//Şimdi view'a yollayabiliriz: | |
return View::make('frontend.index') | |
->with('data', $query) | |
->with('pagination', $pagination); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment