0
votes

I am trying to out all users on my admin dashboard by parsing the result through to my route

Here is my controller;

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;


class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('notAdmin');
    }

    public function index(){

        $user = User::all()->orderBy('id', 'desc')->paginate(100);
        return view('admin-dashboard')->with('users', $users);
    }
}

I get the "Method Illuminate\Database\Eloquent\Collection::orderBy does not exist." error

1
You have a mistake on your Return, change $users on $user, and you dont need ::all()Saromase

1 Answers

2
votes

You have applied wrong query:

You don't need to add all() as you are using paginate

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\User;


class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('notAdmin');
    }

    public function index(){

        $users = User::orderBy('id', 'desc')->paginate(100);
        return view('admin-dashboard')->with('users', $users);
    }
}