我在Laravel應用程序中的兩個模型中存在關係問題。我的模型是:問題與Laravel有很多關係
class Invoice extends Eloquent {
protected $table = 'invoices';
public function line_items()
{
return $this->hasMany('LineItem');
}
}
和:
class LineItem extends Eloquent {
protected $table = 'line_items';
public function invoice()
{
return $this->belongsTo('Invoice');
}
}
在我的控制,我取一個Invoice
行下列要求:
$invoice = Invoice::find($id);
但是,如果我嘗試訪問line_items
財產要獲取與我的發票相關的LineItem
行,我收到以下錯誤:
Invalid argument supplied for foreach()
這是爲什麼?我已經把我的型號多達按Laravel的文檔:http://laravel.com/docs/eloquent#one-to-many
哪裏是'foreach'循環測試?你在表格中是否有正確的鍵/字段來建立關係? –
在我的控制器和視圖中嘗試了:'foreach($ invoice-> line_items as $ line_item)'觸發錯誤。 –
嘗試'dd($ invoice-> line_items)'它不是一個數組,而是一個標量值。 –