Reputation: 983
I am using firstOrCreate() to find the first invoice or create it. This code was working last week then all of a sudden stopped working.
What is working: A database entry IS generated & there are no errors.
The problem: firstOrCreate returning empty. {}
The code:
$invoice = Invoice::firstOrCreate(
[
'user' => $user->id,
'package' => $package->id,
'term' => $pricing->term,
'paid' => 0
], [
'amount' => $pricing->price,
'hash' => 'omittedforSO',
'coupon' => null
]
)->with(['billingInfo', 'package', 'price', 'coupon']);
Upvotes: 1
Views: 1852
Reputation: 983
You have to call with()
before firstOrCreate()
$invoice = Invoice::with(['billingInfo', 'package', 'price', 'coupon'])->firstOrCreate(
[
'user' => $user->id,
'package' => $package->id,
'term' => $pricing->term,
'paid' => 0
], [
'amount' => $pricing->price,
'hash' => 'notforSO',
'coupon' => null
]
);
Upvotes: 2
Reputation: 2129
According to firstOrCreate and your eager loading, he should either find and return the first model of Invoice
where the arguments (first array) match with extra attributes as arrays, containing the relationships you've written, or create an Invoice
with the values of your second argument.
Having that said, it shouldn't return an empty array, ever, unless you've edited Laravel's core code. If you var_dump (dd
) that result ($invoice
), it will either give you a Querybuilder or an Eloquent model (with its list of attributes with null values).
Make sure your fields are in the fillable array of the model Invoice
Upvotes: 0