public function store(array $data, array $categories)
 {
     $product = collect($data);
     if (!$product->has('name') || !$product->has('photo') && !($product->get('id', 0) > 0)) {
         throw new \InvalidArgumentException('The product array should have at least name, and photo fields.');
     }
     $model = Product::findOrNew($product->get('id', null));
     $model->name = $product->get('name');
     $model->photo = $product->get('photoName', $model->photo);
     $model->model = $product->get('model', null);
     $model->save();
     $model->categories()->sync($categories, false);
 }
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit($id)
 {
     $categories = Categories::all();
     $product = Product::findOrNew($id);
     return view('admin.product.edit', compact('categories'))->with(compact('product'));
 }