Advertisement
Guest User

update

a guest
Oct 16th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1.  
  2. public function update(Request $request, $id)
  3. {
  4. // make new variable input request
  5. $input = $request->all();
  6. // customize rules validation
  7. $rules = array(
  8. 'produk_nama' => 'required|string',
  9. 'produk_beli' => 'required|integer',
  10. 'produk_jual' => 'required|integer',
  11. 'supplier_id' => 'required|integer',
  12. 'kategori_id' => 'required|integer',
  13. 'produk_gambar' => 'mimes:jpeg,png,gif,svg'
  14. );
  15.  
  16. // customize error messages
  17. $messages = array(
  18. 'required' => 'This field can not be blank.'
  19. );
  20.  
  21. // apply the validation with rules
  22. $this->validate($request, $rules, $messages);
  23.  
  24. // update data into database depends on id if no errors
  25. // get the data with id
  26. $produk = Produk::FindOrFail($id);
  27.  
  28. // edit upload config
  29. $file = $request->file('produk_gambar');
  30. if ($file != "")
  31. {
  32. //rename the image name
  33. $image = time().'.'.$file->getClientOriginalExtension();
  34. //path image
  35. $path = 'images/produk/';
  36. // apply the proceed
  37. $upload = $file->move($path, $image);
  38. // remove the replacing image
  39. $old_image = $produk->produk_gambar;
  40. if (file_exists($path.$old_image))
  41. {
  42. @unlink($old_image);
  43. }
  44. }
  45. else{
  46. $image = $produk->produk_gambar;
  47. }
  48. //define value of produk_gambar
  49. $produk->produk_gambar = $image;
  50. // update
  51. $produk->update($input);
  52.  
  53. //return with alert
  54. return $produk;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement