laravel中的更新图像无法正常工作

时间:2018-06-21 07:37:50

标签: laravel image intervention

我正在尝试在laravel项目中更新图像,但不会保存图像,我正在使用intervention package

这是我使用dd的结果 enter image description here

控制器:

 public function update(Request $request, $id)
{
    //
    $this->validate($request, [
        'student_name'=>'required|max:50',
        'lead_status'=>'required|max:50',
        'lead_source'=>'required|max:50',
        'avatar' =>'image',



        ]);


        $leads = Lead::findOrFail($id);
        $leads->student_name = $request->student_name;
        $leads->student_nric = $request->student_nric;
        $leads->gender = $request->gender;
        $leads->religion = $request->religion;
        $leads->race = $request->race;
        $leads->date_of_birth = $request->date_of_birth;
        $leads->Address = $request->Address;
        $leads->last_school_attended= $request->last_school_attended;
        $leads->last_grade_completed = $request->last_grade_completed;
        $leads->grade_appliying = $request->grade_appliying;



        if($request->hasFile('avatar')){

            $image=$request->file('avatar');
            $filename=time() . '.' . $image->getClientOriginalExtension();
            $location=public_path('images/' .$filename);
            Image::make($image)->resize(300, 300)->save($location);

            $leads->image=$filename;


            $leads->save();
       }
       else{


        $leads->save();

        session()->flash('notif','Application Saved     Successfully');
      return view('leads.edit')->with('leads', $leads);
    }

}

编辑刀片视图

    <div class="row">
<div class="col-sm-4 form-group">
<img src="/uploads/avatars/{{$leads->image}}" style="width:150px;height:150px;border-radius:50px;">

<form enctype="multipart/form-data" action="{{route('leads.update', $leads->id)}}" method="POST" >
{{ csrf_field() }}
{{ method_field('PUT') }}
<label> Upload Image</label>
<input type="file" name="avatar">
<input type="hidden"  name="_token" value="{{ csrf_token() }}">

</div>


<div class="col-sm-12">
   <h4 text-muted>CHILD'S INFORMATION</h3>
   <hr>

                  <div class="row">
                        <div class="col-sm-4 form-group">

                            <label>FULLNAME</label>

                            <input class="form-control" style="text-transform: uppercase;" type="text" name="student_name" value="{{$leads->student_name}}" placeholder="Enter FULLNAME.."  autocomplete="off">

                        </div>

注意:如果图像显示为空,我将始终以null值结束。

............................................... ................................................... .....................................

1 个答案:

答案 0 :(得分:0)

在编辑刀片中添加具有数据库变量的隐藏输入类型 并将您隐藏的输入名称的值置于else条件下进行保存。

<input type="hidden" name="old_image_name" 
value="@if(isset($admin_details)){{$admin_details->image}} @endif"/>