集合列存在,但不知何故同时不存在



我在这个错误上已经有几个小时了,我不能自己解决这个问题。

这是代码:


$repeated_entry = Product_Market::where(' product__id ', '=', $product->id)→在哪里(‘market_id’,‘=’,美元市场→id)→();

$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'

]);
}

错误提示属性[amount_requested]在此集合实例上不存在。但它确实存在

如果我放了"DD($repeated_entry);"在第一个if之前,为了查看集合,我得到这个

输入图片描述

我可以看到"amount_requested"就在那里,它确实在集合中,它可能是完全明显的,我只是需要一些睡眠,但我想请求一些帮助,(不介意代码的质量,我是一个试图学习的新手)

我尝试了其他方法来获得集合中的值,但它需要保持一个集合与其他代码一起工作,我期待着睡觉,也许我在早上理解一些我看不到rn的东西,对不起,愚蠢的问题

变化:

$repeated_entry_update = Product_Market::where('produto_id', '=', $product->id)
->where('market_id', '=', $market->id);

$repeated_entry_update = Product_Market::where([
'produto_id' => $product->id,
'market_id' => $market->id
])->first();

你需要添加->first()来实际获得对象,清除一点where条件。

$repeated_entry是一个Collection实例。我猜你需要第一个条目。

请在get之后使用first
$repeated_entry = Product_Market::where('produto_id', '=', $product->id)
->where('market_id', '=', $market->id)
->get();
$first_entry = $repeated_entry->first();

然后,如果有匹配的条目,改变条件:

if ($first_entry) {

最新更新