public function postRubah($id) { // validasi $input = Input::all(); $rules = array('cover' => 'mimes:jpg,jpeg,png|max:5000', 'judul' => 'required|max:100', 'penulis' => 'max:100', 'penerbit' => 'max:50', 'tahun' => 'integer', 'jumlah' => 'required|integer'); $validasi = Validator::make(Input::all(), $rules); // tidak valid if ($validasi->fails()) { // pesan $cover = $validasi->messages()->first('cover') ?: ''; $judul = $validasi->messages()->first('judul') ?: ''; $penulis = $validasi->messages()->first('penulis') ?: ''; $penerbit = $validasi->messages()->first('penerbit') ?: ''; $tahun = $validasi->messages()->first('tahun') ?: ''; $jumlah = $validasi->messages()->first('jumlah') ?: ''; $status = ''; return Response::json(compact('cover', 'judul', 'penulis', 'penerbit', 'tahun', 'jumlah', 'status')); // valid } else { // ada cover if (Input::hasFile('cover')) { // data $buku = Buku::set($id); // jika buku mempunyai cover maka hapus cover yang dulu if ($buku->cover) { unlink(public_path() . '/foto/buku/' . $buku->cover); } // nama cover baru $cover = date('dmYHis') . '.png'; // unggah cover baru ke dir "foto/buku" Input::file('cover')->move('foto/buku', $cover); // tidak ada cover } else { $cover = null; } // input $judul = trim(ucwords(Input::get('judul'))); $penulis = trim(ucwords(Input::get('penulis'))); $penerbit = trim(ucwords(Input::get('penerbit'))); $tahun = trim(Input::get('tahun')); $jumlah = trim(Input::get('jumlah')); // rubah data di basisdata Buku::rubah($id, $cover, $judul, $penulis, $penerbit, $tahun, $jumlah); } }