【问题标题】:Kyslik/column sortable package, sort is not working but pagination is okayKyslik/column 可排序包,排序不工作但分页没问题
【发布时间】:2020-01-04 13:45:02
【问题描述】:

型号:

<?php

namespace App;

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

class Exam_sched extends Model
{
    use Sortable;

    protected $fillable = ['date', 'startTime', 'endTime', 'roomNo', 'batch_id', 'subject_id'];

    public $sortable = [
        'id', 'date', 'startTime', 'endTime', 'roomNo', 'batch_id', 'subject_id', 'created_at'
    ];

     public function subject(){
      return $this->belongsTo('\App\Subject','subject_id');
    }
    public function batch(){
      return $this->belongsTo('\App\Batch','batch_id');
    }
}

控制器:

public function index()
    {
        $exam_scheds= Exam_sched::sortable()->paginate(3);
        return view('examschedule',compact('exam_scheds'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $exam_scheds=Exam_sched::sortable()->paginate(3);
        $subjects=Subject::all();
        $batches=Batch::all();
        return view('examschedule', compact('exam_scheds','subjects','batches'));
    }

查看刀片:

<div class="row schedule-form">
        <div class="col-lg-10 offset-lg-1">
            <table class="table table-striped ">
                <thead>
                    <tr class="thead">
                        <th style="width: 150px">@sortablelink('Exam Date')</th>
                        <th style="width: 100px">@sortablelink('Batch')</th>
                        <th style="width: 100px">@sortablelink('Subject')</th>
                        <th style="width: 110px">@sortablelink('Room No')</th>
                        <th>@sortablelink('Start Time')</th>
                        <th>@sortablelink('End Time')</th>
                        <th>@sortablelink('Created At')</th>
                            @auth
                            @if(Auth::user()->role_id == 1)
                        <th>Action</th>                     
                            @endif
                            @endauth

                    </tr>
                </thead>
                <tbody>
                    @if($exam_scheds->count())
                    @foreach($exam_scheds as $exam_sched)
                    <tr class="tbody">
                        <td>{{$exam_sched->date}}</td>
                        <td>{{$exam_sched->batch->name}}</td>
                        <td>{{$exam_sched->subject->name}}</td>
                        <td>{{$exam_sched->roomNo}}</td>                        
                        <td>{{$exam_sched->startTime}}</td>
                        <td>{{$exam_sched->endTime}}</td>   
                        <td>{{$exam_sched->created_at->diffForHumans()}}</td>                   

                    </tr>
                    @endforeach
                    @endif
                </tbody>
            </table>
                 {!! $exam_scheds->appends(\Request::except('page'))->render() !!}
        </div>      

应用程序.php:

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */
        Kyslik\ColumnSortable\ColumnSortableServiceProvider::class,

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,


    ],

Kyslik/column 可排序包,排序不工作,但分页没问题。 这里的任何人请帮助我...谢谢!

Kyslik/column 可排序包,排序不工作,但分页没问题。 这里的任何人请帮助我...谢谢! Kyslik/column 可排序包,排序不起作用,但分页还可以。 这里的任何人请帮助我...谢谢!

【问题讨论】:

    标签: laravel sortables


    【解决方案1】:

    @sortablelink 第一个参数是您在数据库中的列名,第二个参数是将在您的视图中呈现的名称。

    请参见此处的示例: https://github.com/Kyslik/column-sortable-example/blob/L5.8/resources/views/user.blade.php

    所以你的@sortablelink('Exam Date') 应该是@sortablelink('date', 'Exam Date')

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 2022-09-28
      • 1970-01-01
      • 2010-11-05
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多