0

我正在尝试在首次创建用户时创建推荐网址。我的用户模型中的函数如下所示:

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

提前感谢您的帮助=]

ps:如果有更好的方法来创建推荐网址,请告诉我。

更新

我的整个用户模型

<?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
4

1 回答 1

0

通常,该creating方法应在以下范围内调用boot()

public static function boot() {
    parent::boot();

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

然后在第一次保存模型之前自动调用它。

我在您的代码中看到的问题是您正在尝试修改尚不存在的关系。

因此,解释一下,该hasOne方法将尝试将当前模型连接到ReferralUrlSQL 中的远程模型(在您的情况下为模型),但在您保存模型之前它不能这样做,因为您的模型在数据库中不存在

在您第二次尝试时,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;
    }
}
于 2015-10-02T21:01:54.323 回答