1. 程式人生 > >laravel多表多關聯同時更新

laravel多表多關聯同時更新

   public function store(Request $request)
    {
        //商品新增
        $product = Product::create($request->all());//本表
        //分類
        $product->categories()->sync($request->category_id);//多對多關聯更新
        //相簿
        foreach ($request->imgs as $v){
            $img=['img'=>$v];
            $product->product_galleries()->create($img);//一對多更新
        }

        return redirect('/admin/shop/products');
    }