app/Http/Controllers/SliderController.php
/**
	 * Store a newly created resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @return \Illuminate\Http\Response
	 */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request) {
		$this->validate($request, [
			'nama' => 'required',
			'deskripsi' => 'required',
			'foto' => 'file|image|mimes:jpeg,png,jpg|max:2048',
		]);
		try {
			$slider = new Slider();
			$slider->nama = $request->nama;
			$slider->deskripsi = $request->deskripsi;
			$slider->uuid = Uuid::uuid4()->getHex();
			$nama = preg_replace('/\s+/', '', $slider->nama);
			if ($request->hasFile('foto')) {
				$file = $request->file('foto');
				$filename = $nama . '-foto.' . $file->getClientOriginalExtension();
				$path = $request->file('foto')->storeAs('public/foto-slider', $filename, 'local');
				$slider->foto = '/storage' . substr($path, strpos($path, '/'));
				$slider->save();
			}
			$slider->save();
		} catch (Exception $e) {
			DB::rollback();
			Flash::error('Gagal Tambah Data');
		}
		return redirect()->route('slider.index')->with('success', 'Slider successfully added');
	}
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'nama' => 'required' , 'deskripsi' => 'required' , 'foto' => 'file|image|mimes:jpeg,png,jpg|max:2048' , ] ) ; try { $ slider = new Slider ( ) ; $ slider -> nama = $ request -> nama ; $ slider -> deskripsi = $ request -> deskripsi ; $ slider -> uuid = Uuid :: uuid4 ( ) -> getHex ( ) ; $ nama = preg_replace ( '/\s+/' , '' , $ slider -> nama ) ; if ( $ request -> hasFile ( 'foto' ) ) { $ file = $ request -> file ( 'foto' ) ; $ filename = $ nama . '-foto.' . $ file -> getClientOriginalExtension ( ) ; $ path = $ request -> file ( 'foto' ) -> storeAs ( 'public/foto-slider' , $ filename , 'local' ) ; $ slider -> foto = '/storage' . substr ( $ path , strpos ( $ path , '/' ) ) ; $ slider -> save ( ) ; } $ slider -> save ( ) ; } catch ( Exception $ e ) { DB :: rollback ( ) ; Flash :: error ( 'Gagal Tambah Data' ) ; } return redirect ( ) -> route ( 'slider.index' ) -> with ( 'success' , 'Slider successfully added' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// dd($request->file('foto')); # code...