laravel 查询构建器上的first() 函数返回一个stdClass,意思是标准类。
在 php 中的 stdClass 中没有名为 update() 的函数。您在 stdClass 上调用了update(),这会导致错误。
有几种方法可以实现您的目标。
- 使用 Laravel 查询生成器
update() 函数。
$resultQuery = DB::table('cards')->where('api_id', $card->id)->first();
if (your_condition) {
Db::table('cards')
->where('api_id', $card->id)
->update([
'price_usd' => $card->prices->usd
]);
}
- 如果不想取卡数据,请不要拨打
first()
$resultQuery = DB::table('cards')->where('api_id', $card->id);
if (your_condition) {
$resultQuery
->update([
'price_usd' => $card->prices->usd
]);
}
- 使用 Eloquent 模型(Laravel 的首选方式)
为卡片创建一个 Eloquent 模型(如果你还没有这样做的话)。
public class Card extends Model
{
}
使用 eloquent 查询构建器来获取数据。并使用模型update()函数更新数据。
$resultingCard = Card::where('api_id', $card->id)->first();
if (your_condition) {
$resultingCard->update([
'price_usd' => $card->prices->usd,
]);
}