Kyslik / column-sortable

Package for handling column sorting in Laravel 5/6/7/8
MIT License
644 stars 105 forks source link

Method Illuminate\Database\Eloquent\Collection::sortable does not exist. #108

Closed epixian closed 5 years ago

epixian commented 5 years ago

Likely related to closed issue #46 --> Eloquent doesn't seem to recognize Sortable as a trait. I'm not new to PHP, but I am somewhat of a novice at Laravel and Composer so I'm guessing my issue is configuration related. My repo is here.

Using Laravel 5.7.19. Composer found the library with no issues, pulled in version 5.7.0 of this library and performed composer update. composer.json correctly shows requiring 5.7.* of this library. Since using version >=5.5, I assumed that the ColumnSortableServiceProvider would be registered and visible in config/app.php, but no changes in that file after publishing configuration, so I inserted the config following the manual installation instructions and republished. I get the same error in tinker if calling sortable() on a manual query.

model:

use Kyslik\ColumnSortable\Sortable;

class Jobsite extends Model
{
    use Sortable;
    ...

    public $sortable = [
        'name',
        'address',
        'city',
        'state',
        'postal_code',
        'country',
        'acreage',
        'linear_feet',
        'type',
        'status',
        'organization_id'
    ];
    ...

controller:

    public function index()
    {
        try {

            $jobsites = Jobsite::all()->sortable('name');
            return view('jobsites.index', compact('jobsites'));

        } catch (\Kyslik\ColumnSortable\Exceptions\ColumnSortableException $e) {

            dd($e);

        }
    }

On the other hand, @sortablelink('name', 'Name') works as expected in my view, so the Blade extension seems to be working (using unmodified model/controller code).

Kyslik commented 5 years ago

Hey @epixian! What a nice and detailed bug report. I think this is a composer issue; please do try:


The problem is the usage; you need to use scope on the model not on the collection :) so in your case it should be:

Jobsite::sortable('name')->get();
epixian commented 5 years ago

@Kyslik quick reply! It looks like I tossed you a red herring, but you were able to figure it out. Thank you so much, it works now!