search

Home  >  Q&A  >  body text

Laravel's question about the firstOrCreate method. If it is judged whether the returned model is a new one or an original one,

laravel Questions about firstOrCreate method

If it exists, the original data will be returned,

If it does not exist, it will be created

Then how do you know whether what he returns is the original one or the new one?

大家讲道理大家讲道理2868 days ago499

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-16 16:50:21

    wasRecentlyCreatedProperties.

    $user = User::firstOrCreate($userData);
    if($user->wasRecentlyCreated){
        // 新用户处理
        
    }

    reply
    0
  • Cancelreply