Laravel 5 未定义的属性:App\Models\Invoice::$created\u by

Laravel 5 未定义的属性:App\Models\Invoice::$created\u by,laravel-5,Laravel 5,发票模型 protected $fillable = ['invoice_number', 'created_by', 'coupon_id', 'total_transaction', 'service_charge']; public function created_by() { return $this->belongsTo('App\User', 'created_by'); } 用户模型 protected $fillab

发票模型

    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
堆栈跟踪


我哪里做错了?

将列更改为由\u id创建的
,并设置关系也解决了我的问题