當我嘗試將關聯類別與事務關聯時,我無法解決此錯誤。Laravel關係查詢/查找 - 嘗試獲取非對象的屬性
// TransactionController
$transactions = DB::table('transactions')
->where('status', 'false')
->orderBy('date','asc')
->get();
foreach($transactions as $data) {
$transaction = new Transaction();
$transaction->id = $data->id;
$transaction->category = Transaction::find($data->categories_id)->category;
$transaction->description = $data->description;
}
在時間出現的錯誤: ErrorException在TransactionController.php線80: 試圖讓非對象
Line 80: $transaction->category = Transaction::find($data->categories_id)->category;
的財產,但如果我測試我的代碼與死亡,結果如下:
die(Transaction::find($data->categories_id)->category()->first());
{"id":1,"users_id":1,"description":"Alimenta\u00e7\u00e3o","created_at":"2016-11-15 20:31:11","updated_at":"2016-11-15 20:31:11"}
//事務模型
class Transaction extends Model
{
public function category(){
return $this->hasOne('App\Category','id');
}
的
你能'dd'的內容'$ transactions' –
'Transaction :: find($ data-> categories_id) - > category'你爲什麼試圖通過'categories_id'找到一個'Transaction'? :/ – Doom5
檢查所有'$ data-> categories_id',有些可能沒有事務ID。 –