FareedR
4 months ago

Update data with current image file using same form request from store function

Posted 4 months ago by FareedR

how can i use same form request on update function ? it keep display to me " Image field is required " but i dont want to update image , i just want to update other data only . not image .

// form request
public function rules()
    {
        return [
            'title' => 'required|min:6',
            'image' => 'required|image|mimes:png|dimensions:width=1920,height=1076',
            'description' => 'required|min:6',
            'status' => 'required'
        ];
    }
//controller
 public function updateBanner(BannerFormRequest $request,$id)
    {
        $banner = Banner::findOrFail($id);

        if($request->hasFile('image')){
            $oldImage = Storage::disk('public')->delete('{{ $banner->image }}');
            $image = $request->file('image')->store('banners','public');
        }

        $banner->title = $request->get('title');
        $banner->description = $request->get('description');
        $banner->status = $request->get('status');
        $banner->save();

        toastr()->success('Banner has been updated');
        return redirect()->route('list-banner');
    }
//blade
<div class="block-content">
            <form action="{{ route('update-banner', $banner->id) }}" method="post" enctype="multipart/data">
            @csrf
            @method('PATCH')
                <div class="form-group">
                    <label for="input-title">Title</label>
                    <input type="text" class="form-control" name="title" value="{{ $banner->title }}">
                </div>
                <div class="form-group">
                    <label for="input-image">Image</label>
                    <input type="file" class="form-control" name="image">
                    <div class="img">
                        <img class="img-fluid" style="width:50%" src="{{ Storage::url($banner->image) }}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="input-description">Description</label>
                    <input type="text" class="form-control" name="description" value="{{ $banner->description }}">
                </div>
                <div class="form-group">
                    <label for="input-status">Status</label>
                    <select name="status" class="form-control">
                        @if($banner->status)
                        <option value="0">Inactive</option>
                        <option value="1" selected>Active</option>
                        @else
                        <option value="0" selected>Inactive</option>
                        <option value="1">Active</option>
                        @endif
                    </select>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-alt-primary">Submit</button>
                </div>
            </form>
        </div>

Please sign in or create an account to participate in this conversation.