首页 > 解决方案 > 当我在 laravel 中更新时,图像不会被删除

问题描述

当我想单击关闭按钮时,我想使用 jquery 删除图像在我单击编辑按钮以更新数据后图像消失,图像没有被删除,它再次显示。这是我可以使用的代码。请帮忙。提前致谢。

这是我的控制器代码。

public function update(UpdateLocationHint $request, $id)
{
    $hint = LocationHint::findOrFail($id);
    $hint->location_id = $request->input('location_id'); // MB check the location_id from model Location

    $hint->title = $request->input('title');
    $hint->hint_text = $request->input('hint_text');
    $hint->hint_solution_text = $request->input('hint_solution_text');

    if (is_null($hint->hint_text)) {
        $hint->hint_text = '';
    }

    if (is_null($hint->hint_solution_text)) {
        $hint->hint_solution_text = '';
    }

    $imageFile = $request->file('hint_image_file');
    $videoFile = $request->file('hint_video_file');

    if ($imageFile != null) {
        $fileName = md5(uniqid()).'.'.$imageFile->getClientOriginalExtension();
        $uploadDirectoryPath = public_path('storage/uploads/hints/images');

        $imageFile->move($uploadDirectoryPath, $fileName);
        $hint->hint_image_path = $fileName;
    }

    if ($videoFile != null) {
        $fileName = md5(uniqid()).'.'.$videoFile->getClientOriginalExtension();
        $uploadDirectoryPath = public_path('storage/uploads/hints/videos');

        $videoFile->move($uploadDirectoryPath, $fileName);
        $hint->hint_video_path = $fileName;
    }

    $res = $hint->save();

    if ($res != false) {
        flash()->success('Successfully updated!');
    } else {
        flash()->error('Something wrong with saving!');
    }
    return redirect()->route('hints.edit', ['id' => $hint->id]);
}

这是更新视图刀片。

 @if($hint->imageExists())

    <div class="form-group">

        <div class="image_uploaded_block">

            <img style="width: 200px" src="{{url('/storage/uploads/hints/images/'.$hint->hint_image_path)}}" alt="{{ $hint->hint_image_path }}"/>

            <div class="delete_image">

                <a href="" class="btn btn-danger delete_image_btn"><i class="fa fa-times"></i></a>

            </div>

        </div>

    </div>

    @endif

这是jquery代码。

       $(document).ready(function() {
        $('.delete_image_btn').on('click', function (e) {
            e.preventDefault();
            $('.image_uploaded_block').remove();

            $('.image_attributes').attr('disabled', false).attr('readonly', false);
        });

标签: jquerylaravel-5

解决方案


尝试这个

if ($imageFile != null) {
    $fileName = md5(uniqid()).'.'.$imageFile->getClientOriginalExtension();
    $uploadDirectoryPath = public_path('storage/uploads/hints/images');
    $imageFile->move($uploadDirectoryPath, $fileName);
    $hint->hint_image_path = $fileName;
} else {
    $hint->hint_image_path = "";
}

推荐阅读