skip to Main Content

What should I do if I want to exclude the first row in retrieving the users table?

    public function index()
{
    $users = User::all();
    return view('users.users',['users'=> $users]);
}

4

Answers


  1. if you want to make static you can do this

       public function index()
    {
        $users = User::all();
        $first_key = $users->keys()->first();
        $users = $users->forget($first_key);
        return view('users.users',['users'=> $users]);
    }
    
    Login or Signup to reply.
  2. Use the Laravel Collection Skip method: documentation

    public function index()
    {
        $users = User::all()->skip(1);
        return view('users.users',['users'=> $users]);
    }
    
    Login or Signup to reply.
  3. You may use the skip with get() methods to skip a given number of results in the query:

    public function index()
    {
        $users = User::get()->skip(1);
        return view('users.users',['users'=> $users]);
    }
    
    Login or Signup to reply.
  4. if you want to Skip first record (row 1 "where id = 1");
    for that I have used the following logic which is work fine for me.

    $users = User::where('id','>',1)->get();
    

    OR
    you can also use offset method.

    $users = DB::table('users')->offset(1)->get();
    

    https://laravel.com/docs/9.x/queries

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search