Laravel get single row by id

Updated at 21-Sep-2021, By samar

Laravel get single row by id

Hello everyone, in this post we will look at how to solve "Laravel get single row by id" in programming.

You can get single row data by using the primary id of the table. You have to use the find() method to get the specific record which you want to access.
  • Laravel find method on eloquent collection with if statement

    //Laravel find method with id
    $user = App\Models\User::find(1);
    if($user){
        echo "User exists";
    }
    dd($user);
    
    //Find method with an array of ids
    $userIds = [1,5,10];
    $users = App\Models\User::find($userIds);
    if(count($users)){
        echo "Users exists";
    }
    dd($users);
    

Back to code snippet queries related laravel