2015-10-02 63 views
0

我正在嘗試在首次創建用戶時創建引薦網址。 我的我的用戶模型內部功能如下:如何在創建事件時調用模型函數? Laravel-5

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:如果有一個更好的方式去建立轉診網址,請告訴我。

更新

我的整個用戶模型

<?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 
+0

什麼是用戶模型的其餘部分是什麼樣子? – samlev

+0

我更新了整個用戶模型的帖子 – joejoeso

回答

0

通常情況下,creating方法應在0​​內調用:

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

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

然後在之前自動調用模型首次保存。

我在代碼中看到的問題是,您試圖修改尚不存在的關係

這樣解釋,該hasOne方法將嘗試加入當前模型遙控模型(在你的情況下,ReferralUrl模型)的SQL,但它不能做保存您的模型,因爲模型沒有按之前在數據庫中不存在

隨着你的第二次嘗試中,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; 
    } 
} 
+0

謝謝。我更新了我的帖子。當我在我的控制器中調用該函數時,它將保存到我的數據庫中,但我也遇到了錯誤。 – joejoeso

+0

將'$ referral_url = $ this-> referral_url;'(第2行)從您的更新函數中取出。 – samlev

+0

啊我看到了,因爲我正在實例化一個新的引用obj,我並不需要它。 – joejoeso

相關問題