未定义属性:App \ Models \ Invoice :: $ created_by

时间:2017-06-19 08:20:09

标签: 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 $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

我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

将列更改为python2并设置关系也解决了我的问题。