Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function update(Request $request, $id)
- {
- // make new variable input request
- $input = $request->all();
- // customize rules validation
- $rules = array(
- 'produk_nama' => 'required|string',
- 'produk_beli' => 'required|integer',
- 'produk_jual' => 'required|integer',
- 'supplier_id' => 'required|integer',
- 'kategori_id' => 'required|integer',
- 'produk_gambar' => 'mimes:jpeg,png,gif,svg'
- );
- // customize error messages
- $messages = array(
- 'required' => 'This field can not be blank.'
- );
- // apply the validation with rules
- $this->validate($request, $rules, $messages);
- // update data into database depends on id if no errors
- // get the data with id
- $produk = Produk::FindOrFail($id);
- // edit upload config
- $file = $request->file('produk_gambar');
- if ($file != "")
- {
- //rename the image name
- $image = time().'.'.$file->getClientOriginalExtension();
- //path image
- $path = 'images/produk/';
- // apply the proceed
- $upload = $file->move($path, $image);
- // remove the replacing image
- $old_image = $produk->produk_gambar;
- if (file_exists($path.$old_image))
- {
- @unlink($old_image);
- }
- }
- else{
- $image = $produk->produk_gambar;
- }
- //define value of produk_gambar
- $produk->produk_gambar = $image;
- // update
- $produk->update($input);
- //return with alert
- return $produk;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement