Exemplo n.º 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(NewsRequest $request)
 {
     try {
         $request->merge(array('news' => 1));
         if ($request->hasFile('file')) {
             $fileInfo = $this->uploadFile($request->file('file'), 'news');
             if ($fileInfo) {
                 $ship = $this->ship->create($request->only($this->dataGet), $fileInfo['original-name']);
                 //save info file
                 $file = new FileRepository(new File());
                 $fileInfo['ship_id'] = $ship->id;
                 $file->create($fileInfo);
             }
         } else {
             $this->ship->create($request->only($this->dataGet));
         }
         return redirect()->back();
     } catch (Exception $e) {
         return redirect()->back()->withInput()->with('error', 'Xãy ra lỗi khi thêm dữ liệu');
     }
 }
Exemplo n.º 2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(ShipRequest $request, $id)
 {
     try {
         if ($request->hasFile('file')) {
             $fileInfo = $this->uploadFile($request->file('file'), 'ships');
             // if upload success
             if ($fileInfo) {
                 $ship = $this->ship->update($id, $request->only($this->dataGet), $fileInfo['name']);
                 // update file table if isset
                 if (isset($ship->file)) {
                     $file = $ship->file;
                     $file->update($id, $fileInfo);
                 } else {
                     //save info file
                     $file = new FileRepository(new File());
                     $fileInfo['ship_id'] = $ship->id;
                     $file->create($fileInfo);
                 }
             }
         } else {
             $this->ship->update($id, $request->only($this->dataGet));
         }
         return redirect()->back();
     } catch (Exception $e) {
         return redirect()->back()->withInput()->with('error', 'Không thể truy vấn dữ liệu');
     }
 }