2017-11-08 2 views
0

Ich habe eine Laravel-Anwendung mit CRUD-Operationen und ein Problem mit der Aktualisierung des hochgeladenen Bildes. Jedes andere Feld wird aktualisiert. Ich kann nicht für das Leben von mir das Problem finden. HierLaravel 5.4 Bildaktualisierung funktioniert nicht

ist der Code für den Controller:

public function update(Request $request, $id) 
{ 
    // Validate the data 
    $post = Post::find($id); 

    $this->validate($request, array(
      'title' => 'required|max:255', 
      'slug' => "required|alpha_dash|min:5|max:255|unique:posts,slug,$id", 
      'category_id' => 'required|integer', 
      'body' => 'required', 
      'featured_image' => 'image' 
     )); 

    // Save the data to the database 
    $post = Post::find($id); 

    $post->title = $request->input('title'); 
    $post->slug = $request->input('slug'); 
    $post->category_id = $request->input('category_id'); 
    $post->body = Purifier::clean($request->input('body')); 


    if ($request->hasFile('featured_img')) { 
     //add the new photo 
     $image = $request->file('featured_img'); 
     $filename = time() . '.' . $image->getClientOriginalExtension(); 
     $location = public_path('images/' . $filename); 
     Image::make($image)->resize(800, 400)->save($location); 
     $oldFilename = $post->image; 

     //update the database 
     $post->image = $filename; 

     //delete the old photo 
     Storage::delete($oldFilename); 

    } 

    $post->save(); 

    if (isset($request->tags)) { 
     $post->tags()->sync($request->tags); 
    } else { 
     $post->tags()->sync(array()); 
    } 


    // set flash data with success message 
    Session::flash('success', 'This post was successfully saved.'); 

    // redirect with flash data to posts.show 
    return redirect()->route('posts.show', $post->id); 
} 

Ich erhalte die Flash-Mitteilung zu sagen, dass die Post erfolgreich aktualisiert hat, aber das Bild ist immer noch die gleiche und hat nicht einmal hochgeladen zu die Datenbank. Ich benutze Laravel 5.4. Namen in Form nicht gerade Namen im Controller übereinstimmen -

{!! Form::model($post, ['route' => ['posts.update', $post -> id], 'method' => 'PUT', 'files' => true]) !!} 
<div class="col-md-8"> 


    <!-- form model binding --> 
    {{ Form::label('title', 'Title:') }} 
    {{ Form::text('title', null, ['class' => 'form-control input-lg']) }} 

    {{ Form::label('slug', 'Slug:', ['class' => 'form-spacing-top']) }} 
    {{ Form::text('slug', null, ['class' => 'form-control input-lg']) }} 

    {{ Form::label('category_id', 'Category:')}} 
    {{ Form::select('category_id', $categories, null, ['class' => 'form-control']) }} 

    {{ Form::label('tags', 'Tags:', ['class' => 'form-spacing-top']) }} 
    {{ Form::select('tags[]', $tags, null, ['class' => 'form-control select2-multi', 'multiple' => 'multiple']) }} 

    {{ Form::label('featured_image', 'Update Featured Image:', ['class' => 'form-spacing-top']) }} 
    {{ Form::file('featured_image')}} 

    {{ Form::label('body', 'Body:', ['class' => 'form-spacing-top']) }} 
    {{ form::textarea('body', null, ['class' => 'form-control']) }} 


</div> 
<div class="col-md-4"> 
    <div class="well"> 
     <div class="dl-horizontal"> 
      <dt>Created At:</dt> 
      <dd>{{date('M j, Y h:ia', strtotime($post->created_at)) }}</dd> 
     </div> 

     <div class="dl-horizontal"> 
      <dt>Last Updated:</dt> 
      <dd>{{date('M j, Y h:ia', strtotime($post->updated_at)) }}</dd> 
     </div> 

     <hr> 
     <div class="row"> 
      <div class="col-sm-6"> 
       {!! Html::linkRoute('posts.show', 'Cancel', array($post->id), array('class' => 'btn btn-danger btn-block')) !!} 
      </div> 
      <div class="col-sm-6"> 

       {{ form::submit('Save Changes', ['class' => 'btn btn-success btn-block']) }} 
      </div> 
     </div> 
    </div> 
</div> 

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

Vielleicht wird das Bild nicht hochgeladen. Könnten Sie bitte Ihren Formularcode posten? –

+0

@ JoséA.Zapata da gehen Sie – spbrad

+0

In der Steuerung heißt es: 'if ($ Anfrage-> hasFile ('featured_img'))', aber der Dateiname in der Form ist 'featured_image'. –

Antwort

0

Beantwortet von @ JoséA.Zapata:

aufbereiteter Form Code hinzufügen.

Verwandte Themen