i am at this error for some hours now, and i cant resolve it by myself.
this is the code:
$repeated_entry = Product_Market::where('produto_id', '=', $product->id) ->where('market_id', '=', $market->id)->get();
$repeated_entry_update = Product_Market::where('produto_id', '=', $product->id)
->where('market_id', '=', $market->id);
if($repeated_entry->count())
{
$repeated_entry_update->update(['amount_requested' => $repeated_entry->amount_requested $request->product_amount,
'amount_left' => $repeated_entry->amount_requested $request->product_amount,
]);
}
else
{
product_market::create(['produto_id' => $product['id'],
'market_id' => $market['id'], //saves the info ghatered to the product_market relationship
'amount_requested' => $request->product_amount, //table
'amount_left' => $request->product_amount,
'amount_sold' => '0'
]);
}
the error says Property [amount_requested] does not exist on this collection instance. but it does exist
if i put a "DD($repeated_entry);" before the first if, to see the collection i get this
i can see the "amount_requested" RIGHT THERE, it is indeed in the collection, it might be completly obvious, and i just need some sleep, but i thought of asking for some help, (and dont mind the quality of the code, i am a noobie trying to learn)
ive tried other ways to get to the value in the collection, but it needs to stay a collection to work with the rest of the code, and i am expecting to sleep and maybe i undestand something in the morning that i cant see rn, sorry for the dumb question
CodePudding user response:
$repeated_entry
is a Collection instance. I assume you need the first entry. You need to use first
after get
.
$repeated_entry = Product_Market::where('produto_id', '=', $product->id)
->where('market_id', '=', $market->id)
->get();
$first_entry = $repeated_entry->first();
Then, change the condition if there's a matching entry:
if ($first_entry) {
CodePudding user response:
Change:
$repeated_entry_update = Product_Market::where('produto_id', '=', $product->id)
->where('market_id', '=', $market->id);
to
$repeated_entry_update = Product_Market::where([
'produto_id' => $product->id,
'market_id' => $market->id
])->first();
You need to add ->first()
to actually get the object, cleared up the where condition a bit.