为什么不更新Laravel中的类别名称和图像?

时间:2018-12-03 15:57:19

标签: php mysql laravel-5

使用Laravel 5.6和MySQL。我要使用以下控制器功能更新类别表中的类别名称和图像?

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

    if ($request->isMethod('get'))
        return view('categories.form', ['image' => Category::find($id)]);

    else {
        $rules = [
            'categoryname' => 'required',
        ];
        $this->validate($request, $rules);
        $image = Category::find($id);
        if ($request->hasFile('image')) {
            $dir = 'images/';
            if ($image->image != '' && File::exists($dir . $image->image))
                File::delete($dir . $image->image);
            $extension = strtolower($request->file('image')->getClientOriginalExtension());
            $fileName = str_random() . '.' . $extension;
            $request->file('image')->move($dir, $fileName);
            $image->categoryimage = $fileName;
        } elseif ($request->remove == 1 && File::exists('images/' . $image->image)) {
            File::delete('images/' . $image->post_image);
            $image->categoryimage = null;
        }
    }
    $image->categoryname = $request->description;
    $image->save();
    return redirect()->route('categories.index');
}

和路线

Route::match(['get', 'put'], 'category/update/{id}', 'CategoryController@update');

并编辑表单

 @if(isset($image))
        <form method="PUT" action="http://localhost:8000/category/update/{{$image->id}}" enctype="multipart/form-data">

        <input type="hidden" name="_method" value="put">

<label for="description" class="col-form-label col-md-3 col-lg-2">Description</label>
                <div class="col-md-8">
                    <input class="form-control" autofocus placeholder="Description" name="description" type="text" id="description" value="{{ isset($image) ? $image->categoryname : '' }}">



 <label for="image" class="col-form-label col-md-3">Image</label>
                    <div class="col-md-5">
                        <img id="preview"
                             src="{{asset((isset($image) && $image->categoryimage!='')?'images/'.$image->categoryimage:'images/noimage.png')}}"
                             height="200px" width="200px"/>
                        <input class="form-control" style="display:none" name="image" type="file" id="image" name="_token" value="{{ csrf_token() }}">
                        <br/>
                        <a href="javascript:changeProfile();">Add Image</a> |
                        <a style="color: red" href="javascript:removeImage()">Remove</a>
                        <input type="hidden" style="display: none" value="0" name="remove" id="remove">

但是当我尝试更新数据时,它没有更新。仅刷新到同一页面。不,有任何错误。我该如何解决这个问题?

0 个答案:

没有答案