public function update($id) { $rules = array('code' => 'required', 'name' => 'required', 'price' => array('required', 'regex:/^\\d*(\\.\\d{2})?$/'), 'categories' => 'required', 'product_image' => 'mimes:jpeg,bmp,png'); $validator = Validator::make(Input::all(), $rules); // process the login if ($validator->fails()) { return Redirect::to('admin/product/' . $id . '/edit')->withErrors($validator)->withInput(); } else { // store $filename = ""; if (Input::hasFile('product_image')) { if (Input::file('product_image')->isValid()) { Input::file('product_image')->move(ProductController::imagePath()); $filename = Input::file('product_image')->getClientOriginalName(); } } Product::where('code', '=', $id)->update(['code' => Input::get('code')]); Product::where('code', '=', $id)->update(['name' => Input::get('name')]); if ($filename !== "") { Product::where('code', '=', $id)->update(['image' => ProductController::imagePath() . $filename]); } Product::where('code', '=', $id)->update(['price' => Input::get('price')]); $product = Product::where('code', '=', $id)->firstOrFail(); foreach (Category::all() as $cat) { DB::table('products_category')->where('product_id', '=', $product->code)->where('category_id', '=', $cat->id)->softDeletes(); } foreach (Input::get('categories') as $catId) { DB::table('products_category')->insert(array('product_id' => $product->code, 'category_id' => $catId)); } // redirect Session::flash('message', 'Successfully updated product!'); return Redirect::to('admin/product'); } }
<th>Name</th> <th>Price</th> <th>Image</th> <th>Categories</th> <th>Actions</th> </tr> </thead> <tbody> @foreach($products as $product) <tr> <td>{{$product->code}}</td> <td>{{$product->mark()->getResults()->name}}</td> <td>{{$product->name}}</td> <td>{{$product->price}}</td> <?php $image = str_replace(ProductController::imagePath(), "", $product->image); ?> <td><img src="{{asset('/web/images/product/phpOP4Umz')}}" style="width: 50px; height: 50px;"/></td> <td> <?php $count = 0; foreach ($product->categories()->getResults() as $category) { if ($count < count($product->categories()->get()) - 1) { echo $category->name . ', '; } else { echo $category->name; } $count++; } ?> </td>