2017-06-19 21 views
0

發票模型

protected $fillable = ['invoice_number', 'created_by', 'coupon_id', 'total_transaction', 'service_charge']; 

    public function created_by() 
    { 
     return $this->belongsTo('App\User', 'created_by'); 
    } 

用戶模型

protected $fillable = [ 
     'name', 'email', 'password', 'address', 'phone', 'user_status_id', 'email_activation_token', 'role_id' 
    ]; 

    public function invoice() 
    { 
     return $this->hasMany('App\Models\Invoice', 'created_by'); 
    } 

用戶有很多發票,發票屬於用戶

這裏是我的控制器

$invoice = new Invoice(); 
$invoice->invoice_number = '#INV' . $latestInvoiceId + 1; 
$invoice->created_by()->associate(Auth::user()); 
$invoice->total_transaction = $invoice_total; 
$invoice->save(); 

但是我得到這個

ErrorException in BelongsTo.php line 91: 
Undefined property: App\Models\Invoice::$created_by 
in BelongsTo.php line 91 

堆棧跟蹤

https://paste.laravel.io/1XQQp

我哪裏做錯了嗎?

回答

0

更改列爲created_by_id並設置關係也解決了我的問題。

相關問題