尝试从数据库输出所有用户详细信息时出现“找不到对象”错误

时间:2019-05-14 13:17:44

标签: laravel eloquent

我试图通过解析结果到我的路线来淘汰管理控制台上的所有用户

这是我的控制人;

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);
    }
}

我得到“方法Illuminate \ Database \ Eloquent \ Collection :: orderBy不存在”。错误

1 个答案:

答案 0 :(得分:2)

您应用了错误的查询:

使用分页时不需要添加all()

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);
    }
}