예제 #1
0
 public function postTambah()
 {
     // 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'));
     } else {
         // ada cover
         if (Input::hasFile('cover')) {
             // nama cover
             $cover = date('dmYHis') . '.png';
             // unggah cover 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'));
         // tambah data di basisdata
         Buku::tambah($cover, $judul, $penulis, $penerbit, $tahun, $jumlah);
     }
 }