jeudi 7 mars 2019

Image is not removed when i update in laravel

I want to remove the image using with jquery when i want to click the close button image is disappear after i click on the edit button to update the data the image is not deleted it shows again. Here is my code that can I use.Please help. Thanks in advance.

Here is my Controller code.

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]);
}

Here is Update view blade.

 @if($hint->imageExists())

    <div class="form-group">

        <div class="image_uploaded_block">

            <img style="width: 200px" src="" alt=""/>

            <div class="delete_image">

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

            </div>

        </div>

    </div>

    @endif

And here is the jquery code.

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

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire