Edit Operation – Laravel

Edit of any row from the list/table is done by using direct button.  To edit we must take care of following things:

  • Views ===> Edit button ===> href with route and id
  • Controller ===> edit() function retrieve the selected rows and displays over form’s input box
  • Controller ===> update() function to update the data when submit button is pressed. We can update in 2 different ways. Either using bulk method using update() function or using save() function

Views ===> Edit Button

<a href="{{ route('admin.testimonial.edit',['id'=>$test->id]) }}">

<i class="ace-icon fa fa-pencil bigger-130"></i>

</a>

Controller ===> Edit Function

public function edit($id)
{
$testimonial = Testimonial::find($id);
$title = "Edit testimonial";
$update = true;

return view('backend.testimonial-edit',[
'testimonial'=>$testimonial,
'title'=>$title,
'update'=>$update,
'id' =>$id,

]);
}

Views ===> Edit Form

<div class="form-group">
<label class="col-sm-5 control-label no-padding-right" for="form-field-1">Testimonial</label>
<div class="col-sm-7">
<textarea name="testimonial" />{{ $testimonial->testimonial }} </textarea>
</div>
</div>

<div class="form-group">
<label class="col-sm-5 control-label no-padding-right" for="form-field-1"></label>
<div class="col-sm-7">
@if(isset($testimonial->image) && !empty($testimonial->image))
<img height="100px" width="100px" src="{{ asset($testimonial->image) }}">
@endif

<input name="image" type="file" />
</div>
</div>

 

Controller ===> Update Function

    public function update(Request $request, $id)
    {
        $data = $request->validate([
            'status'=>'required',
            'name'=>'required|max:20',
            'address_designation'=>'required|max:20',
            'testimonial'=>'required|min:5|max:200',
           ],[
            'name.max' => "Name must not exceed 10 character",
            'address_designation.max' => "Address/Designation must not exceed 10 character",
            'testimonial.max' => "Testimonial must be written in between 5 to 200 characters",
        ]);
        if($request->hasFile('image'))
        {
            $testimonial = Testimonial::find($id);
           $oldFilePath = $testimonial->image;
            if(file_exists(public_path().'/'.$oldFilePath))
            {
                 unlink(public_path().'/'.$oldFilePath);
            }
           // $data['image'] = $this->uploadFile($request->file('image'),'')); // To Local public folder
            $data['image'] = $this->uploadFile($request->file('image'),'img/testimonial'); // To Storage folder inside given path
            //$data['image'] = $this->uploadFile($request->file('image'),app_path('img/testimonial/')); // To Any Folder we choose from root project folder
        }
        $testimonial = Testimonial::find($id);
//dd($data);
        $testimonial->update($data);
        return redirect()->back()->withSuccess("Testimonial updated successfully");
    }

Leave a Reply

Your email address will not be published. Required fields are marked *