I'm not able to fix this error when I tried associate category to transaction.
// 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;
}
The error occurs at time: ErrorException in TransactionController.php line 80: Trying to get property of non-object
Line 80: $transaction->category = Transaction::find($data->categories_id)->category;
But, if I test my code with die, that's result:
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"}
// Transaction Model
class Transaction extends Model
{
public function category(){
return $this->hasOne('App\Category','id');
}
[]'s