我如何在创建事件上调用模型函数?Laravel-5


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

当用户首次创建时,我试图创建一个推荐url。我的User模型中的函数是这样的:

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

在模型中,我试过这样做,但没有工作

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

我还尝试在我的用户控制器中调用它在创建用户操作

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

我得到这个错误返回

Indirect modification of overloaded property App'User::$referral_url has no effect

提前感谢你们的帮助=]

p。s:如果有更好的方法来创建推荐url,请告诉我。

我的整个用户模型

<?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->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');
    }
}

我修改了模型中的函数,现在看起来像这样。

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;
    }
}

当我呼叫

$user->make_url() 

我可以创建它,它显示在我的数据库中,但我也得到错误-

Trying to get property of non-object

通常creating方法应该在boot()中调用:

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

在第一次保存模型之前,会自动调用

我看到你的代码的问题是,你试图修改一个关系还不存在

因此,为了解释,hasOne方法将尝试将当前模型加入到SQL中的远程模型(在您的情况下是ReferralUrl模型),但在您保存模型之前它不能这样做,因为您的模型在数据库中不存在

在第二次尝试中,ReferralUrl对象是正在更改的对象,因此这是您需要保存的对象:

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;
    }
}