多个图像更新Laravel已删除

时间:2017-11-02 07:46:51

标签: image laravel edit

嗨,如果有人可以帮助我,我有更新图像的代码,当我上传另一个图像时它工作正常,我不触摸其他图像,例如我更改的第一个图像,我上传了另外三个图像我没有触摸,我点击提交编辑按钮,当我回到编辑视图时,我只能看到我改变的一张照片,其他三张照片都消失了。

这是刀片:

  <form class="form-horizontal" method="POST"
            action="{{ action('website\questions\MatchLinesWithPhotosController@updateQuestion', ['id' => $question->id]) }}"
            enctype="multipart/form-data">
            {{ csrf_field() }}

            @include('website.questions.general-update-header')

            <div class="row">
                <div class="col-lg-5">
                    <label style="padding-left: 10px">
                        <h4 class="box-title">@lang('general.options_for_answers'):</h4>
                        <em class="text-caption" id="caption-question-type"> *@lang('general.chose_correct_multiple_choice_photo')</em>
                    </label>
                </div>
            </div>

            <div class="optionsFormMatchlinePhotos">

                      @for($i=0; $i<=(count($answers->where('deleted', 0))/2)-1; $i++)

              <div class="col-md-6" id="option{{ $i }}">
                <div class="row">
                    <div class="col-md-6 col-md-offset-2">
                        <?php $option = $answers->where('deleted', 0)->where('order', $i)->where('is_key', 1)->first(); ?>

                        <div class="info-box answer-with-photo-option">

                           <img src="{{url('/')}}/images/answers/{{ $option->id }}/medium/{{ $option->image_path }}" class="answer-image-create" id="image{{ $i }}">

                           </div>     @if($option)<input onchange="readURL(this, {{ $i }});" type="file" name="image{{ $i }}" id="<?php echo 'answer_file'.$i;?>" style="
                           margin-top: -19%;
                           padding-bottom: 9%;
                           " />
                           @endif
                       </div>
                   </div>
                   <div class="row">
                    <div class="form-group {{ $errors->has('options.'.$i) ? ' has-error': '' }}">
                        <div class="col-md-12">
                           <div class="col-md-3" style="width: 20%;">


                             {{--    <input  type="text" name="options[{{ $i }}]" value="{{ $i }}" style="display :none" /> --}}
                         </div>
                         <input  type="text" name="matchanswer[{{ $i }}]" value="{{ $i }}" style="display :none" />

                         <?php $match = $answers->where('deleted', 0)->where('order', $i)->where('is_key', 0)->first(); ?>


                         <div class="col-md-5">
                             @if($match)      
                             <input type="text" name="match{{ $i }}" value="{{$match->text}}"
                             class="form-control" placeholder="@lang('general.match') {{ $i + 1}}">
                             @else 
                             <input type="text" name="match{{ $i }}" value=""
                             class="form-control" placeholder="@lang('general.match') {{ $i + 1}}">
                             @endif

                             @if($errors->has('match.'.$i))
                             <div class="col-md-12"></div>
                             <span class="help-block">
                                <strong>{{ $errors->first('match.'.$i) }}</strong>
                            </span>
                            @endif


                        </div>

                        <div class="col-md-4">
                            <button type="button" value="{{ $i }}" class="btn btn-flat btn-default btn-sm" id="delete_option" title="@lang('buttons.remove_option')">
                                <i class="fa fa-trash-o" aria-hidden="true"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>


        @endfor
    </div>

这是编辑控制器:

 public function editQuestionForm($id){
        $question = Question::findOrFail($id);
        $answers  = MatchLineAnswer::where('question_id',  $id)->get();
        return view('website.questions.partials.matchlineswithphotos.edit-matchlineswithphotos',compact('question', 'answers'));
        return session('message');
    }

    public function updateQuestion(MatchLinesWithPhotosFormRequest $request, $id){
            $QuestionController = new QuestionController();
            $QuestionController->UpdateQuestion($request, $id);

            MatchLineAnswer::where('question_id', $id)->update(['deleted' => 1]);
 $to_delete_answers = MatchLineAnswer::select('id')->where('question_id', $id)->get();
        MatchLineAnswer::where('question_id', $id)->update(['deleted' => 1]);

        $imgController = new ImagesController();
        $element = 'answers';

        foreach($to_delete_answers as $deleted_answer){
            $imgController->deleteImage($element, $deleted_answer->id);
        }

        foreach ($request['matchanswer'] as $key => $option){
                 $answer_create =   MatchLineAnswer::create([
                        'question_id' => $id,
                        'order'  => $option,

                        'is_key' => 1
                    ]);

                   $img = $request->file('image'. $option);

            $create_answer_id = $answer_create->id;

            $imgController->uploadAnswerImage($img, $element, $create_answer_id);

            $answer_update = MatchLineAnswer::where('id', $create_answer_id)->update(['image_path'=> $imgController->getUniqueNameAttribute()]);
                    MatchLineAnswer::create([
                        'question_id' => $id,
                        'order'  => $option,
                        'text'   => strip_tags($request['match'.$option]),
                        'is_key' => 0
                    ]);
                }
        $test_id = Question::where('id',  $id)->first();
        session()->flash('message', 'Question successfully updated');
        return redirect()->action('website\tests\CompleteTestController@showAllTestInfo', ['test_id' => $test_id->test_id]);
    }

0 个答案:

没有答案