示例#1
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(BeritaRequest $request)
 {
     $destinationPath = public_path('upload/berita');
     $data = $request->except('file');
     if ($request->has('data')) {
         $data = json_decode($request->get('data'));
     }
     if (isset($data->id_berita)) {
         $berita = Berita::find($data->id_berita);
         if ($request->hasFile('file')) {
             $checkfile = file_exists($destinationPath . '/' . $berita->gambar);
             if ($checkfile) {
                 unlink($destinationPath . '/' . $berita->gambar);
             }
             $berita->gambar = $request->file('file')->getClientOriginalName();
             $request->file('file')->move($destinationPath, $berita->gambar);
         }
     } else {
         $berita = new Berita();
         $berita->tanggal = date('Y-m-d');
         $berita->waktu = date('H:i:s');
         if ($request->hasFile('file')) {
             $berita->gambar = $request->file('file')->getClientOriginalName();
             $request->file('file')->move($destinationPath, $berita->gambar);
         }
     }
     $berita->judul_berita = $data->judul_berita;
     $berita->isi = $data->isi;
     $berita->author = $this->auth->user()->nama_pegawai;
     if ($berita->save()) {
         return response()->json(array('success' => TRUE));
     }
 }