Firstorcreate not working for new laravel entry

I am new to laravel and using core to create models I have below base class for all models.

class MainModel extends Ardent {
  //with some common methods
}

      

and now I have a submodel class of such a user

class User extends MainModel
{
  //with user table and other methods related to user
 }

      

and also i have a repository for user -> User Repository and i call firstOrcreate method to check (user record) and create user. if the user exists in the database, then the above method works fine, its returning an existing user object, but if the user does not exist in the database, it does not create the user and insert it into the table. Am I doing wrong here?

+3


source to share


1 answer


You just need to save the user after the call firstOrCreate()

:

$user->save();

      



You can check if the user exists before this:

if (!$user->id)  $user->save();

      

+4


source







All Articles