Answers for "laravel eloquent first or new"

PHP
2

laravel eloquent get first

// return first row by id
$user = App\User::where('id',$id)->first();
// or return directly a field
$userId = App\User::where(...)->pluck('id');
Posted by: Guest on May-17-2020
1

eloquent firstOrCreate

firstOrCreate() will automatically create a new entry in the database if there is not match found. Otherwise it will give you the matched item.
firstOrNew() will give you a new model instance to work with if not match was found, but will only be saved to the database when you explicitly do so (calling save() on the model). Otherwise it will give you the matched item.
Posted by: Guest on November-08-2020
1

eloquent first

$user = User::where('mobile', Input::get('mobile'))->get();

if (!$user->isEmpty()){
    $firstUser = $user->first()
}
Posted by: Guest on August-07-2020
-1

create new record via model in laravel

$userData = array('username' => 'Me', 'email' => '[email protected]');
User::create($userData);
Posted by: Guest on October-19-2020

Browse Popular Code Answers by Language