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\Query\Builder::sortable does not exist. #111

Closed datomnurdin closed 5 years ago

datomnurdin commented 5 years ago

I'm trying to populate the data and try to sort out. But it seems I got this error message.

BadMethodCallException
Method Illuminate\Database\Query\Builder::sortable does not exist.

It happened when I updated it here

class UserController extends Controller
{
    public function list($id)
    {
        $user = new User();

        if($id == 1){
            $user->role = "Patient";
        } else if($id == 2){
            $user->role = "Doctor";
        } else if($id == 3){
            $user->role = "Pharmacy";
        } else if($id == 4){
            $user->role = "Admin";
        }

        $users = DB::table('users')->where('users.role',$id)->sortable()->paginate(10)->get(); //here when I got an error message
        return View::make('user.all')->with('users', $users)->with('user', $user);
    }

Please advice. TQ.

Kyslik commented 5 years ago

Hey, @datomnurdin. The package works only with models (eloquent models) not with database query builder.

Basically you want to read more on the models topic and understand the difference between using model and query builder, the gist of what you need is:

$users = User::sortable()->paginate(); // no ->get()! because thats included in the paginate() already
datomnurdin commented 5 years ago

Thanks. It works.