title: How do I call model function on create event? Laravel-5
body: <p>I'm trying to create a referral url when a user is first created.
  My function inside my User model looks like this:</p>
  
  <pre><code>private function make_url()
  {
      $url = str_random(40);
      $this-&gt;referral_url-&gt;url = $url;
      if ($this-&gt;save()){
          return true;
      }
      else{
          return false;
      }
  }
  </code></pre>
  
  <p>Within the model, I've tried doing this but didn't work</p>
  
  <pre><code>USER::creating(function ($this){
  $this-&gt;make_url();
  })
  </code></pre>
  
  <p>I also tried calling it in my User Controller within the create user action </p>
  
  <pre><code>public function create(UserRequest $request)
  {
  $data = $request-&gt;all()
  $data['password']= bcrypt($request-&gt;input('password'));
  
  if($user=User::create($data))
  {
    $user-&gt;make_url();
  }
  }
  </code></pre>
  
  <p>I get this error in return</p>
  
  <pre><code>Indirect modification of overloaded property App\User::$referral_url has no effect
  </code></pre>
  
  <p>Thanks in advance for your help guys =]</p>
  
  <p>p.s: If there's a better way to go about creating referral urls please tell me.</p>
  
  <p><strong>update</strong></p>
  
  <p>My entire user model</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Auth\Authenticatable;
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Auth\Passwords\CanResetPassword;
  use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
  
  class User extends Model implements AuthenticatableContract, CanResetPasswordContract
  {
      use Authenticatable, CanResetPassword;
  
      protected $table = 'users';
  
      protected $fillable = [
                              'first_name',
                              'last_name',
                              'url',
                              'email',
                              'password',
                              'answer_1',
                              'answer_2',
                              'answer_3'
                              ];                           
  
  
      protected $hidden = ['password', 'remember_token'];
  
  
      public function make_url()
      {
          $url = str_random(40);
          $this-&gt;referral_url-&gt;url = $url;
          if ($this-&gt;save()){
              return true;
          }
          else{
              return false;
          }
      }
  
      public function user_info()
      {
          return $this-&gt;hasOne('App\UserInfo');
      }
  
      public function sec_questions()
      {
          return $this-&gt;hasOne('App\SecurityQuestions');
      }
  
      public function referral_url()
      {
          return $this-&gt;hasOne('App\ReferralUrl');
      }
  }
  </code></pre>
  
  <p><strong>update</strong>
  I modified the function in the model to look like this now.</p>
  
  <pre><code>public function make_url()
  {
      $url = str_random(40);
      $referral_url = $this-&gt;referral_url;
      $referral_url = new ReferralUrl();
      $referral_url-&gt;user_id = $this-&gt;id;
      $referral_url-&gt;url = $url;
      if ($referral_url-&gt;save()){
          return true;
      }
      else{
          return false;
      }
  }
  </code></pre>
  
  <p>When I call </p>
  
  <pre><code>$user-&gt;make_url() 
  </code></pre>
  
  <p>I'm able to create it and it shows up in my db, but I also get the error-</p>
  
  <pre><code>Trying to get property of non-object
  </code></pre>