Изображение не удаляется при обновлении в laravel - PullRequest
0 голосов
/ 07 марта 2019

Я хочу удалить изображение с помощью 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);
        });

1 Ответ

1 голос
/ 07 марта 2019

Попробуйте это

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 = "";
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...