title: Call to a member function create() on null in laravel using Socialite
body: <p>When I try to authenticate the user with using facebook so I am able to store data into the user table but the failure to create data into the soical_accounts.So after that errors with come up "call to a member funtion create() on null". Can anyone provide me solution that where I am wrong.    </p>
  
  <p>In my SocialAccountControll, I have the following method</p>
  
  <pre><code> public function handleProviderCallback($provider)
      {
          try{
  
              $user = Socialite::driver($provider)-&gt;user();
          } catch (Exception $e) {
                 return redirect('/login');
          }
  
          $authUser = $this-&gt;findOrCreateUser($user, $provider);
  
          Auth::login($authUser, true);
  
          //  redirectTo, so that way we use the same redirect location that the rest of our authentication uses.
           //This is a normal protected function that you can add in your users table to redirect a user wherever
           // you want to set that redirect to. 
          //return redirect($this-&gt;redirectTo);
  
          return redirect('/home');
      }
  
      public function findOrCreateUser($socialUser, $provider)
      {
  
                $account = SocialAccount::where('provider_name', $provider)-&gt;where('provider_id',$socialUser-&gt;getId())-&gt;first();
  
                if($account)
                {
                    return $account-&gt;user;
                }
                else
                {
                    $user = User::where('email', $socialUser-&gt;getEmail())-&gt;first();
                    if(! $user)
                    {
                        $user = User::create([
                                'email' =&gt; $socialUser-&gt;getEmail(),
                                'name' =&gt; $socialUser -&gt;getName()
                        ]);
                    }
  
                    $user-&gt;accounts()-&gt;create([
                         'provider_name' =&gt; $provider,
                         'provider_id' =&gt; $socialUser-&gt;getId()
                    ]);
  
                    return $user;
                }
  
  
      }
  </code></pre>
  
  <p>In my database migration, I have users and social_accounts and user have one to many relationship with social_accounts.</p>
  
  <p><strong>user table</strong>:</p>
  
  <pre><code>public function up()
  {
      Schema::create('users', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;string('email')-&gt;nullable();
          $table-&gt;timestamp('email_verified_at')-&gt;nullable();
          $table-&gt;string('password')-&gt;nullable();
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p><strong>Social_accounts table:</strong></p>
  
  <pre><code>public function up()
      {
          Schema::create('social_accounts', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;bigInteger('user_id');
              $table-&gt;string('provider_name')-&gt;nullable();
              $table-&gt;string('provider_id')-&gt;unique()-&gt;nullable();
              $table-&gt;timestamps();
          });
      }
  </code></pre>
  
  <p>User Model 
  class User extends Authenticatable
  {
      use Notifiable;</p>
  
  <pre><code>/**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
      'name', 'email', 'password',
  ];
  
  /**
   * The attributes that should be hidden for arrays.
   *
   * @var array
   */
  protected $hidden = [
      'password', 'remember_token',
  ];
  
  public function accounts()
  {
      $this-&gt;hasMany('App\SocialAccount');
  }
  </code></pre>
  
  <p>}</p>
  
  <p>SocialAccount Model
  
  
  <p>namespace App;</p>
  
  <p>use Illuminate\Database\Eloquent\Model;</p>
  
  <p>class SocialAccount extends Model
  {
      protected $fillable = [
          'provider_name', 'provider_id'
        ];</p>
  
  <pre><code>  public function user() {
      return $this-&gt;belongsTo('App\User');
    }
  </code></pre>
  
  <p>}</p>