Hello I am having this problem with the laravel eloquent with.
This code (in the docs) $debtHistory = DebtHistory::with('user.firstname')->get();
seems to not work, because it returns something lik Call to undefined relationship [firstname] on model [App\User].
what am I doing wrong here? Any idea? Thank you guys. :)
Note: If this helps.
I actually have an alternative method but I would like to do it in one query. This code my help you to know what I am trying to do.
$debtHistory = DebtHistory::whereBetween('created_at',[$filterData['from'], $filterData['to']])->where('amount', '>', '0')->get();
$excelData = [];
foreach($debtHistory as $history) {
array_push($excelData, ['Name' => $history->user->firstname .' '. $history->user->lastname, 'Debt' => $history->amount, 'Date' => Carbon::parse($history->created_at)->format('Y-m-d')]);
}
I want to do that process in a eloquent model query is that possible?
This error says that User
model doesn't have firstname
relationship defined. You should load the data like this:
$debtHistory = DebtHistory::with('user')->get();
And display it with something like this:
@foreach ($debtHistory as $one)
{{ $one->user->firstname }}
@endforeach
This will work if user()
has belongsTo()
relationship.