Help us understand the problem. What is going on with this article?

laravelのcolumn-sortableでマルチカラムソートする

More than 1 year has passed since last update.

やりたいこと

前提

laravelのページネーションにソート機能を追加する

やった

こんな感じでオリジナルのソートメソッド作れるからそれでやる。

Model

カスタムソートメソッドを追加

Reservation.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;

class Reservation extends Model
{
    use Sortable;

    public $sortable = ['id', 'reserve_date', 'time_from'];

    /**
     * 日付と時刻でorderByするメソッドを追加
     * xxxSortableの形にする
     */
    public function reserveDateTimeSortable($query, $direction)
    {
        // ORDER BY reserve_date ASC/DESC, time_from ASC/DESC
        return $query->orderBy('reserve_date', $direction)->orderBy('time_from', $direction);

        // 日付だけ昇順/降順切り替えで時間は常に昇順にする場合
        // ORDER BY reserve_date ASC/DESC, time_from ASC
        // return $query->orderBy('reserve_date', $direction)->orderBy('time_from', 'asc');
    }
}

View

引数のカラム名を作ったメソッド名にする

reservation/index.php
<th>@sortablelink('reserveDateTime', '予約日時')</th>

備考

<th>@sortablelink('reserve_date, time_from', '予約日時')</th> とかでいけんじゃね?

ダメでした。
今のところ基本機能ではマルチカラムソート対応してなさそう。
ソートのカスタマイズが簡単だからそこまで問題でもないかな。

haserror
仕事以外なら何でもしますから!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした