php - How do I call model function on create event? Laravel-5 -


i'm trying create referral url when user first created. function inside user model looks this:

private function make_url() {     $url = str_random(40);     $this->referral_url->url = $url;     if ($this->save()){         return true;     }     else{         return false;     } } 

within model, i've tried doing didn't work

user::creating(function ($this){ $this->make_url(); }) 

i tried calling in user controller within create user action

public function create(userrequest $request) { $data = $request->all() $data['password']= bcrypt($request->input('password'));  if($user=user::create($data)) {   $user->make_url(); } } 

i error in return

indirect modification of overloaded property app\user::$referral_url has no effect 

thanks in advance guys =]

p.s: if there's better way go creating referral urls please tell me.

update

my entire user model

<?php  namespace app;  use illuminate\auth\authenticatable; use illuminate\database\eloquent\model; use illuminate\auth\passwords\canresetpassword; use illuminate\contracts\auth\authenticatable authenticatablecontract; use illuminate\contracts\auth\canresetpassword 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->referral_url->url = $url;         if ($this->save()){             return true;         }         else{             return false;         }     }      public function user_info()     {         return $this->hasone('app\userinfo');     }      public function sec_questions()     {         return $this->hasone('app\securityquestions');     }      public function referral_url()     {         return $this->hasone('app\referralurl');     } } 

update modified function in model now.

public function make_url() {     $url = str_random(40);     $referral_url = $this->referral_url;     $referral_url = new referralurl();     $referral_url->user_id = $this->id;     $referral_url->url = $url;     if ($referral_url->save()){         return true;     }     else{         return false;     } } 

when call

$user->make_url()  

i'm able create , shows in db, error-

trying property of non-object 

normally creating method should called within boot():

public static function boot() {     parent::boot();      static::creating(function ($model) {         $model->foo = 'bar';     }); } 

this called automatically before model saved first time.

the problem see code you're attempting modify relation doesn't exist yet.

so explain, hasone method attempt join current model remote model (in case referralurl model) in sql, can't before save model because model doesn't exist in database.

with second attempt, referralurl object 1 changing, 1 need save:

public function make_url() {     $url = str_random(40);     $referral_url = $this->referral_url     $referral_url->url = $url;     if ($referral_url->save()){         return true;     } else {         return false;     } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -