Warm tip: This article is reproduced from stackoverflow.com, please click
crud eloquent laravel php

CRUD Update with image Laravel

发布于 2020-04-23 11:24:12

I am trying to update an already saved record, which includes an image and image path in database. When I update the record, the image gets uploaded and the redirect works and shows success message, however, none of the columns in database get updated with the new data. Why is this happening??

Here is my update method:

public function update(Request $request, $id)
    {

        if ($request['post_image'] != '') {

            $request->validate([
            'title' => 'required',
            'description' => 'required',
            'slug' => 'required',
            'message' => 'required',
            'user' => 'required',
            'post_image' => 'image|mimes:jpeg,png,jpg,gif|max:2048'
            ]);

            $image_path = Storage::disk('public')->putFile('uploads/images', $request->file('post_image'));
            $request['image_path'] = $image_path;

        }else{

            $request->validate([
                'title' => 'required',
                'description' => 'required',
                'slug' => 'required',
                'message' => 'required',
                'user' => 'required'
                ]);
        }  

        $update = [
            'title' =>$request->title,
            'description' =>$request->description,
            'slug' =>$request->slug,
            'message' =>$request->message,
            'user' =>$request->user,
            'post_image' =>$request->image_path];

        Post::where('id', $id)->update($update);

        return Redirect::to('admin')->with('success','Great! Post updated successfully');

    }

Here is the view:

<form action="{{ route('blog.update', $post_info->slug) }}" method="POST" name="edit_post" role="form" enctype="multipart/form-data">
{{ csrf_field() }}
@method('PATCH')

<h1>Edit Post</h1>
<div role="separator" class="dropdown-divider"></div> 

<div class="form-row">
    <div class="form-group col-12 col-md-6">
      <label for="title">Post Title</label>
      <input type="text" autocomplete="off" class="form-control" id="title" name="title" placeholder="Your post title" value="{{$post_info->title}}" required>
      <span class="text-danger">{{ $errors->first('title') }}</span>
    </div>

    <div class="form-group col-12 col-md-6">
        <label for="slug">Slug</label>
      <input type="text" autocomplete="off" class="form-control" id="slug" name="slug" placeholder="Write post slug" value="{{$post_info->slug}}" required>
      <span class="text-danger">{{ $errors->first('slug') }}</span>
    </div>
</div>

<div class="form-row">
    <div class="form-group col-12 col-md-12">
        <label for="description">Post Description</label>
      <textarea class="form-control" id="description" name="description" placeholder="Enter a small description for your post" required>{{$post_info->description}}</textarea>
      <span class="text-danger">{{ $errors->first('description') }}</span>
    </div>


</div>


    <div class="badge badge-warning badge-pill">Message</div>
    <div role="separator" class="dropdown-divider"></div>

 <div class="form-row">
    <div class="form-group col-md-12">
        <textarea class="form-control" col="4" id="message" name="message">{{$post_info->message}}</textarea>
        <span class="text-danger">{{ $errors->first('message') }}</span>
    </div>
    @if($post_info->post_image)
    <img src="{{ asset('storage/' . $post_info->post_image) }}" class="img-thumbnail img-fluid blog-img">
    @endif
    <input id="post_image" type="file" class="form-control" name="post_image">

</div>

<input type="hidden" value="{{ Auth::user()->name }}" name="user">

  <button type="submit" class="btn btn-warning btn-block">Edit Post</button>

</form>
Questioner
Tiago Filipe
Viewed
30
Berto99 2020-02-08 07:05

This line is wrong:

Post::where('id', $id)->update($update);

because where returns a Eloquent Builder object, not Model instance that represent the record you are trying to get from the database... instead you can do:

Post::where('id', $id)->firstOrFail()->update($update); /* or Post::where('id', $id)->first()->update($update); if you are sure that the record is found */

Or if id is the primary key:

Post::findOrFail($id)->update($update); /* or Post::find($id)->update($update); if you are sure that the record is found */