我需要更新与雄辩的具体顺序项目。 我有这个型号:Laravel 5.3:雄辩有很多更新
class Orders extends \Illuminate\Database\Eloquent\Model
{
public $timestamps = false;
protected $fillable = ['items_array'];
public function items()
{
return $this->hasMany(Items::class, 'order_id', 'order_id');
}
public function setItemsArrayAttribute($data)
{
$this->items()->whereIn('article_id', array_map(function($item){
return $item['article_id'];
}, $data['items']))->update($data);
}
}
class Items extends \Illuminate\Database\Eloquent\Model
{
protected $table = 'order_to_items';
public $timestamps = false;
protected $fillable = ['internal_code'];
public function order()
{
return $this->belongsTo(Orders::class, 'order_id', 'order_id');
}
}
我有这样的API响应:
$response = [
'message'=>'some',
'order_id'=>'111-222-333',
'items'=>[
[
'article_id' => 'R-320108',
'internal_code' => 333
],
[
'article_id' => 'R-320116',
'internal_code' => 444
],
]
];
所以我做这个
$order = Orders::where('order_id', $response['order_id'])->with('items')->first();
,我试图让这样的:
$order->update([
'is_sent' => true,
'items_array' => $response['items']
]);
bu这不起作用。有没有办法将相关模型与API响应进行匹配并进行更新?
谢谢!
是否有任何特定的错误,并请提供您的数据库架构? –