2016-09-04 1 views
0

Ich benutze Laravel 5.1 und ich muss Fotos in Ordner/Bilder speichern.Laravel 5.1 Foto mit Formular hinzufügen

Ich habe Speicherfunktion:

public function store(Requests\VoucherRequest $request) 
    { 

     //$photo= null; 
     $file = array('photo' => $request->file('photo')); 
     // setting up rules 
     dd($request->file('photo')); 
     $rules = array('photo' => 'required|image|max:10000'); //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()->back()->withErrors(["photo" => "Photo requirments - format: jpg, jpeg, png | max. size: 1 MB"]); 

     } 
     else { 
    // checking file is valid. 
     if ($request->file('photo')->isValid()) { 
     $destinationPath = public_path().'/images'; // upload path 
     $extension = $request->file('photo')->getClientOriginalExtension(); // getting image extension 
     $photo = str_random(5).'.'.$extension; // renameing image 
     $request->file('photo')->move($destinationPath, $photo); // uploading file to given path 
     // sending back with message 

     } 
     else { 
     return redirect()->back()->withErrors(["photo" => "Photo requirments - format: jpg, jpeg, png | max. size: 1 MB"]); 
     } 
     } 


     $voucher = new Voucher($request->all()); 
     //$article['key']= str_random(30); 
     $voucher['photo'] = $photo; 


     Auth::user()->vouchers()->save($voucher); 

     Alert::success('Voucher has beed added!','Good job!')->persistent("Close"); 



     return redirect('vouchers'); 
    } 

auch ich in create.blade.php haben:

{!! Form::open(['url'=>'vouchers','files' => 'true']) !!} 

       @include('vouchers.form',['submitButtonText'=>'Click to Add New Vocuher']) 

       {!! Form::close() !!} 

und ich habe form.blade.php:

<div class="form-group row del hidden"> 
    <p class="col-md-3 text-right">{!! Form::label('photo','Upload best image of room') !!}</p> 
    <div class="col-md-6"> 
    {!! Form::file('photo', null, ['class'=>'btn btn-info']) !!} 
    </div><br> 

</div> 

<div class="form-group row"> 
    <div class="col-md-12"> 
    <div class="output"><h6 class="text-center">Click here to add image</h6> 
    <p class="text-center">(Allowed format is: jpg, jpeg, png | max. size: 1 MB) 
    <div class="image" class="col-md-3"> 
     <img id="image" style="width:100%;"> 
    </div> 
    </div> 
    </div> 

</div> 

und js code:

  $(function() { 


     $(".output").on("click", function() { 
    $('#photo').trigger('click'); 
}); 
    $("#photo").on("change", function (e) { 
    var image = document.getElementById('image'); 
    image.src = URL.createObjectURL(e.target.files[0]); 
    }); 


}); 

aber jetzt, wenn ich Form-I-Fehler erhalten einreichen klicken ... Ich versuche, dd ($ file) und dann null bekomme ich nur so Foto nicht submited ist ...

Was hier ein Problem ist? Ich sehe es einfach nicht?

+0

Was ist der Fehler? –

+0

gibt es keinen Fehler, ich wähle nur das Foto aus dem Formular, aber wenn Sie versuchen, es einzureichen, ist Foto NULL. Warum? –

Antwort

0
{!! Form::open(['url'=>'vouchers','files' => 'true']) !!} 

zu

{!! Form::open(['url'=>'vouchers','files' => true]) !!} 

Docs

+0

Ich habe es getan. Wieder ist das gleiche Problem ... –