Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel5.3:雄辩有很多更新_Php_Laravel_Eloquent_Relationship_Has Many - Fatal编程技术网

Php Laravel5.3:雄辩有很多更新

Php Laravel5.3:雄辩有很多更新,php,laravel,eloquent,relationship,has-many,Php,Laravel,Eloquent,Relationship,Has Many,我需要更新项目的具体秩序与雄辩。 我有以下型号: class Orders extends \Illuminate\Database\Eloquent\Model { public $timestamps = false; protected $fillable = ['items_array']; public function items() { return $this->hasMany(Items::class, 'order_

我需要更新项目的具体秩序与雄辩。 我有以下型号:

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']
]);
但这不起作用。有没有办法将相关模型与API响应匹配并进行更新

谢谢

您可以使用
save()

更新()


是否存在任何特定错误,请提供您的DB架构?它不起作用,因为$response['items']数组中有两个项。可能有一个匹配函数part@Toletov在这种情况下,只需迭代项目并更新每个项目。这是问题的链接,你能帮我吗?
$order->is_sent = true;
$order->items_array = $response['items'];
$order->save();
$order = Orders::where('order_id', $response['order_id'])
               ->update([
                   'is_sent' => true,
                   'items_array' => $response['items']
               ]);