Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| devweb:laravel:collection [08-04-2019 08:34] – créée edmc73 | devweb:laravel:collection [26-01-2023 17:05] (Version actuelle) – [sort sur plusieurs champs] edmc73 | ||
|---|---|---|---|
| Ligne 14: | Ligne 14: | ||
| $collect-> | $collect-> | ||
| + | |||
| + | ===== pagination ===== | ||
| + | Rendre une collection paginable | ||
| + | |||
| + | Ajouter ce code dans **app/ | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Providers; | ||
| + | |||
| + | use Illuminate\Pagination\LengthAwarePaginator; | ||
| + | use Illuminate\Pagination\Paginator; | ||
| + | use Illuminate\Support\Collection; | ||
| + | use Illuminate\Support\ServiceProvider; | ||
| + | |||
| + | class AppServiceProvider extends ServiceProvider | ||
| + | { | ||
| + | /** | ||
| + | * Bootstrap any application services. | ||
| + | * | ||
| + | * @return void | ||
| + | */ | ||
| + | public function boot() | ||
| + | { | ||
| + | |||
| + | Collection:: | ||
| + | /** @var Collection $this */ | ||
| + | $page = $page ?: (Paginator:: | ||
| + | $lap = new LengthAwarePaginator( | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $perPage, | ||
| + | $page, | ||
| + | $options | ||
| + | ); | ||
| + | $lap-> | ||
| + | return $lap; | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Ensuite, utiliser comme le paginate() de eloquent | ||
| + | |||
| + | <code php> | ||
| + | $data = collect($myArray); | ||
| + | $data = $data-> | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== sort sur plusieurs champs ===== | ||
| + | Si c' | ||
| + | |||
| + | SELECT * FROM `liste_programmes` ORDER BY `date_version` DESC, `order`, `nom` | ||
| + | |||
| + | <code php> | ||
| + | $filtre_programme=collect($filtre_programme)-> | ||
| + | return [ | ||
| + | - strtotime($p-> | ||
| + | $p-> | ||
| + | $p->nom, | ||
| + | ]; | ||
| + | })-> | ||
| + | </ | ||
| + | |||
| + | Trier comme on veut | ||
| + | <code php> | ||
| + | $permissions = $permissions-> | ||
| + | switch ($p-> | ||
| + | case " | ||
| + | return " | ||
| + | break; | ||
| + | case " | ||
| + | return " | ||
| + | break; | ||
| + | case " | ||
| + | return " | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | return " | ||
| + | break; | ||
| + | } | ||
| + | }); | ||
| + | </ | ||