From eacf085a96dda7544a3b988543a58ca92b92b29f Mon Sep 17 00:00:00 2001 From: Eduard Fekete Date: Sat, 25 Feb 2017 16:24:00 +0100 Subject: [PATCH] Mehrfach Foto Upload; Bugs gefixed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Es ist nun möglich mehrere Fotos gleichzeitig hochzuladen, wird jedoch noch nicht validiert! --- index.html | 22 +-- shop/app/Http/Controllers/CarController.php | 134 +++++++++++------- shop/public/css/main.css | 6 +- shop/resources/views/cars/create.blade.php | 56 +++++--- shop/resources/views/cars/showAll.blade.php | 4 +- shop/resources/views/home.blade.php | 13 +- .../views/layouts/partials/errors.blade.php | 16 +-- shop/routes/web.php | 11 +- 8 files changed, 163 insertions(+), 99 deletions(-) diff --git a/index.html b/index.html index da78263..c4c80aa 100644 --- a/index.html +++ b/index.html @@ -35,17 +35,17 @@ - + + diff --git a/shop/app/Http/Controllers/CarController.php b/shop/app/Http/Controllers/CarController.php index bc8efa2..02da256 100644 --- a/shop/app/Http/Controllers/CarController.php +++ b/shop/app/Http/Controllers/CarController.php @@ -8,12 +8,15 @@ use Validator; use Redirect; use Session; +use Storage; use App\Car; use App\Image; class CarController extends Controller { + public $imageCounter = 1; + public function show() { return view('cars.index'); @@ -48,6 +51,38 @@ public function showByTitle($title) return view('car.showByTitle')->with("car", $car); } + + /** + * Filter + * + * @param int $request + * @return \Illuminate\Http\Response + */ + public function filter() + { + $this->validate(request(), [ + 'price' => 'required|numeric', + 'brand' => 'required|alpha', + 'model' => 'required|alpha_dash', + ]); + + if ($this->validate->fails()) { + + Session::flash('error', 'uploaded file is not valid'); + return Redirect::to('/'); + + } else { + echo $this->request->price; + die; + + $matchingCars = Car::where('model', '=', $this->request->model)->get(); + $allImages = Image::all(); + + return view('cars.showAll') + ->with("allCars", $matchingCars) + ->with("allImages", $allImages); + } + } /** * Display the specified resource. @@ -64,75 +99,76 @@ public function showAll() ->with("allImages", $allImages); } - public function store() - { + /** + * + * @param Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { $this->validate(request(), [ - 'title' => 'required|max:80', - 'description' => 'required|min:10', - 'price' => 'required|numeric', - 'brand' => 'required|alpha', - 'model' => 'required|alpha_dash', - 'image' => 'required|image|dimensions:min_width=100,min_height=200' + 'title' => 'required|max:80', + 'description' => 'required|min:10', + 'price' => 'required|numeric', + 'brand' => 'required|alpha', + 'model' => 'required|alpha_dash' ]); $car = new Car(); - $car->title = request('title'); - $car->description = request('description'); - $car->price = request('price'); - $car->brand = request('brand'); - $car->model = request('model'); + $car->title = request('title'); + $car->description = request('description'); + $car->price = request('price'); + $car->brand = request('brand'); + $car->model = request('model'); $car->save(); - $allCars = Car::all(); - $image = $this->upload($car->id); + $allCars = Car::all(); + $allImages = $request->file('image'); + + if(!empty($allImages)): + foreach($allImages as $image): + $image = $this->upload($image, $car->id); + endforeach; + endif; - return redirect('showAll'); + return redirect('cars/showAll') + ->with("allCars", $allCars) + ->with("images", $allImages); } - - public function upload($car_id) + + /** + * + * @param integer $car_id + * @return Image + */ + public function upload($image, $car_id) { - // getting all of the post data - $file = array('image' => Input::file('image')); - - // setting up rules - $rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000 - - // doing the validation, passing post data, rules and the messages - $validator = Validator::make($file, $rules); - - if ($validator->fails()) { - // send back to the page with the input data and errors - return Redirect::to('upload')->withInput()->withErrors($validator); - - } else { - // checking file is valid. - if (Input::file('image')->isValid()) { - + if ($image->isValid()) { + $uploadPath = 'uploads'; // getting image extension - $extension = Input::file('image')->getClientOriginalExtension(); - $fileName = date("Ymdis").'.'.$extension; + $extension = $image->getClientOriginalExtension(); + $fileName = date("Ymdis").$this->imageCounter++.'.'.$extension; // uploading file to given path - Input::file('image')->move($uploadPath, $fileName); - - $image = new Image(); - $image->setCarId($car_id); - $image->setPath( $uploadPath . "/" . $fileName ); - $image->save(); - + $image->move($uploadPath, $fileName); + + $ImageObj = new Image(); + $ImageObj->setCarId($car_id); + $ImageObj->setPath( $uploadPath . "/" . $fileName ); + $ImageObj->save(); + Session::flash('success', 'Upload successfully'); - - return $image; - + + return $ImageObj; + } else { - + // sending back with error message. Session::flash('error', 'uploaded file is not valid'); return Redirect::to('/'); } - } } } diff --git a/shop/public/css/main.css b/shop/public/css/main.css index 41c639c..d2ec605 100644 --- a/shop/public/css/main.css +++ b/shop/public/css/main.css @@ -1,6 +1,6 @@ .carObject { display: inline-block; - border: 2px solid #D8D8D8; + border: 2px solid #ffdbdb; min-height:400px; } @@ -60,6 +60,10 @@ padding-right: 10px; } +p.detailsDescription { + margin-bottom: 0px; +} + table.details tr td { min-width: 80px; diff --git a/shop/resources/views/cars/create.blade.php b/shop/resources/views/cars/create.blade.php index a2c38d3..96dd765 100644 --- a/shop/resources/views/cars/create.blade.php +++ b/shop/resources/views/cars/create.blade.php @@ -2,48 +2,66 @@ @section('content') - {!! Form::open(array('action' => 'CarController@store', 'files'=>true)) !!} +
{{ csrf_field() }} + + @if(Session::has('error')) +

{!! Session::get('error') !!}

+ @endif
- +
- +
- +
- +
- {!! Form::label('model','Modell',array('id'=>'model','class'=>'')) !!} - {!! Form::text('model','',array('id'=>'model','class'=>'', 'placeholder' => 'model')) !!} + +
+
- {!! Form::label('Bild') !!} - {!! Form::file('image', null) !!} - -

{!!$errors->first('image')!!}

- - @if(Session::has('error')) -

{!! Session::get('error') !!}

- @endif +
- - +
- {!! Form::submit('Senden') !!} +
@include('layouts.partials.errors') - {!! Form::close() !!} +
+ + + @endsection \ No newline at end of file diff --git a/shop/resources/views/cars/showAll.blade.php b/shop/resources/views/cars/showAll.blade.php index 5702bba..4d89f91 100644 --- a/shop/resources/views/cars/showAll.blade.php +++ b/shop/resources/views/cars/showAll.blade.php @@ -37,9 +37,7 @@ @foreach($allImages as $image) @if ($car->id === $image->car_id) - - - + @endif @endforeach diff --git a/shop/resources/views/home.blade.php b/shop/resources/views/home.blade.php index f9cb2f7..8dbcd77 100644 --- a/shop/resources/views/home.blade.php +++ b/shop/resources/views/home.blade.php @@ -3,8 +3,16 @@ @section('content')
-
- + + + + {{ csrf_field() }} + + @if(Session::has('error')) +

{!! Session::get('error') !!}

+ @endif + +
@@ -33,4 +41,5 @@
+ @stop \ No newline at end of file diff --git a/shop/resources/views/layouts/partials/errors.blade.php b/shop/resources/views/layouts/partials/errors.blade.php index db0caec..b38bd5c 100644 --- a/shop/resources/views/layouts/partials/errors.blade.php +++ b/shop/resources/views/layouts/partials/errors.blade.php @@ -1,11 +1,11 @@ @if(count($errors)) -
- @endif \ No newline at end of file diff --git a/shop/routes/web.php b/shop/routes/web.php index 1c6fee4..587f9cb 100644 --- a/shop/routes/web.php +++ b/shop/routes/web.php @@ -11,14 +11,13 @@ | */ -Route::get('/', function () { return view('welcome'); }); +Route::get('/', function () { return view('welcome'); }); Route::get('home', function () { return view('home'); }); -//Route::get('/cars', 'CarController@show'); -Route::get('cars/create', 'CarController@create'); -Route::get('cars/showAll', 'CarController@showAll'); -Route::post('cars/filter', 'CarController@filter'); -Route::post('cars/store', 'CarController@store'); +Route::get('cars/create', 'CarController@create'); +Route::get('cars/showAll', 'CarController@showAll'); +Route::post('cars/filter', 'CarController@filter'); +Route::post('cars/store', 'CarController@store'); /*--Footer--------------------------------------*/ Route::get('impressum', function () { return view('footer.impressum'); });