Laravel で ソート機能を実装するには、プラグラインを利用することで、効率的に開発することができます。
たくさんあるプラグインの中で、特にお勧めのプラグインは、column-sortable です。
利用方法もシンプルで、たいへん利用しやすいので、お勧めです。
インストール
1 |
composer require kyslik/column-sortable |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
[hoge@sv sample]$ composer require kyslik/column-sortable Info from https://repo.packagist.org: #StandWithUkraine ./composer.json has been updated Running composer update kyslik/column-sortable Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking kyslik/column-sortable (6.5.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading kyslik/column-sortable (6.5.0) - Installing kyslik/column-sortable (6.5.0): Extracting archive Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi INFO Discovering packages. inertiajs/inertia-laravel .............................................................................................................. DONE kyslik/column-sortable ................................................................................................................. DONE laravel/sail ........................................................................................................................... DONE laravel/sanctum ........................................................................................................................ DONE laravel/tinker ......................................................................................................................... DONE nesbot/carbon .......................................................................................................................... DONE nunomaduro/collision ................................................................................................................... DONE nunomaduro/termwind .................................................................................................................... DONE spatie/laravel-ignition ................................................................................................................ DONE tightenco/ziggy ........................................................................................................................ DONE 81 packages you are using are looking for funding. Use the `composer fund` command to find out more! > @php artisan vendor:publish --tag=laravel-assets --ansi --force INFO No publishable resources for tag [laravel-assets]. Found 1 security vulnerability advisory affecting 1 package. Run composer audit for a full list of advisories. Using version ^6.5 for kyslik/column-sortable |
実装方法
ソート機能を実装する流れは、下記のようになります。
・ModelにSortableを追加
・データ取得時のSortable指定
・View表示
Model
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Kyslik\ColumnSortable\Sortable; // ← 追記 class User extends Model { use Sortable; // ← 追記 } |
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UsersController extends Controller { public function index() { $users = User::sortable()->get(); // ← sortable() を宣言 return view('users.index')->with('users', $users); } ・・・省略・・・ } |
view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<div> <table class="table table-bordered"> <thead> <tr> <th scope="col">@sortablelink('id', 'ID')</th> <th scope="col">@sortablelink('name', '名前')</th> <th scope="col">@sortablelink('tel', '電話番号')</th> <th scope="col">@sortablelink('email', 'メールアドレス')</th> <th scope="col">@sortablelink('created_at', '作成日時')</th> <th scope="col">@sortablelink('updated_at', '更新日時')</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <th scope="row">{{ $user->id }}</th> <td>{{ $user->name }}</td> <td>{{ $user->tel }}</td> <td>{{ $user->email }}</td> <td>{{ $user->created_at }}</td> <td>{{ $user->updated_at }}</td> </tr> @endforeach </tbody> </table> </div> </div> |
参考サイト
Laravel のソート機能の実装については、こちらの記事を参考にさせていただきました。
【Laravel】Sortableを使用して簡単にソート機能を実装
https://qiita.com/anomeme/items/5475c5e8ba9136e73b4e