Некоторые поля не заполнены laravel - PullRequest
0 голосов
/ 19 июня 2020

Здравствуйте, участники сообщества, у меня возникла проблема, которая для меня немного "странная". Небольшой контекст:

Я создаю форму для экзамена, в которой есть вопрос, который сопровождается полями: description, iframe, image, изображение не является обязательным. Эти вопросы, в свою очередь, будут иметь свои поля description, iframe, image.

  • Для этого я сделал то, что в форме создал массив question[] для хранения поля этой таблицы, как это question[description], question[iframe] и question[image]
  • Для ответов примените нечто подобное, двумерный массив answers[][] в одном я сохраняю индекс ответа и в другом поле мои поля: answers[][description], answers[][iframe] и answers[][image]
  • Когда вы пытаетесь вставить только описание и видео вопроса, проблем нет. Проблема заключается в ответах, в которых не все сохраняют поля. Со стороны answers[][description] первое поле сохраняет, второе - нет, третье - нет, четвертое - нет.
  • В answers[][iframe] происходит то же самое, за исключением того, что первое поле не сохраняет , второй - нет, третий - нет, а четвертый - нет.

На следующем изображении проблема лучше видна: введите здесь описание изображения

И пи c, где вы можете увидеть поля вопросов: введите описание изображения здесь

Моя модель Question:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Question extends Model
{
    protected $fillable = [
        'description', 'iframe', 'image' ,'exam_id'
    ];

    protected $guarded = [];

    public function Exam()
    {
        return $this->belongsTo(Exam::class);
    }

    public function answers()
    {
        return $this->hasMany(Answer::class);
    }
}

Моя модель Answers:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Answer extends Model
{    
    protected $fillable = [
        'description', 'iframe', 'question_id'
    ];

    protected $guarded = [];

    public function question()
    {
        return $this->belongsTo(Question::class);
    }
}

QuestionController:

<?php

namespace App\Http\Controllers;

use App\Exam;
use App\Http\Requests\QuestionRequest;
use    Illuminate\Http\Request;
use App\Question;
use function GuzzleHttp\Promise\all;

class QuestionController extends Controller
{
    public function create(Exam $exam)
    {
        return view('question.create', compact(['exam']));
    }

    public function store(Exam $exam, QuestionRequest $request)
    {
        $data = request()->validate([
            'question.description'  => 'required',
            'question.iframe'       => 'required',
            'answers.*.description' => 'required',
            'answers.*.iframe'      => 'required',
        ]);
        dd($data);

        $question = $exam->questions()->create($data['question']);
        $question->answers()->createMany($data['answers']);

        return redirect('/exams/' . $exam->id);
    }
}

Я использую собственный Request файл:

<?php

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;

class QuestionRequest extends FormRequest {
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'question.description'  => 'required',
            'question.iframe'       => 'required',
            'answers.*.description' => 'required',
            'answers.*.iframe'      => 'required',
        ];
    }

    public function messages()
    {
        return[
            'question.description.required' => 'Debes agregar una descripcion al campo Descripcion.',
            'question.iframe.required' => 'Debes agregar el link del video al campo Video Asociado.',
            'answers.*.description.required' => 'Debes agregar una respuesta en el campo Respuesta',
            'answers.*.iframe.required' => 'Debes agregar un video en el campo Video Asociado'
        ];
    }
}

Мои миграции

Answers

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAnswersTable extends Migration{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('answers', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('question_id')->unsigned();

            $table->text('description');
            $table->text('iframe');
            $table->text('image')->nullable();
            $table->timestamps();

            $table->foreign('question_id')->references('id')->on('questions');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('answers');
    }
}

Questions

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateQuestionsTable extends Migration{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(){

        Schema::create('questions', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('exam_id')->unsigned();

            $table->text('description');
            $table->text('iframe');
            $table->text('image')->nullable();

            $table->timestamps(); 

            $table->foreign('exam_id')->references('id')->on('exams');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('questions');
    }
}

на последнем моем просмотре create.blade.php:

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <span>Crear nueva pregunta</span>
                        <a href="/exams/" class="btn btn-secondary btn-sm float-right" onclick="return confirm('Tus datos se perderan ¿Deseas regresar a la pagina principal?')">Regresar</a>
                    </div>
                        <div class="card-body">
                           @if($errors->any())
                                <div class="alert alert-danger">
                                    <h6>Por favor, verifica que los campos esten llenos</h6>
                                </div>
                            @endif

                            <form action="/exams/{{ $exam->id }}/questions" method="POST"  enctype="multipart/form-data">
                                @CSRF

                                <input hidden name="user_id" value="{{ auth()->user()->id }}">
                                <div class="form-group">

                                    <label for="description">Descripcion de la pregunta *</label>
                                    <textarea name="question[description]" type="text" class="form-control" id="description" aria-describedby="descriptionHelp" placeholder="Inserte la pregunta">{{ old('question.description') }}</textarea>
                                    <small id="descriptionHelp" class="form-text text-muted">Escribe la descripcion de la pregunta.</small>


                                    @if($errors->has('question.description'))
                                        <div class="alert alert-danger">
                                            {{ $errors->first('question.description') }}
                                        </div>
                                    @endif
                                </div>

                                <div class="form-group">
                                    <label for="iframe">Video asociado *</label>
                                    <textarea name="question[iframe]" type="text" class="form-control" id="iframe" aria-describedby="iframeHelp" placeholder="Inserte la URL del video">{{ old('question.iframe') }}</textarea>
                                    <small id="iframeHelp" class="form-text text-muted">Inserta la url del video.</small>

                                    @if($errors->has('question.iframe'))
                                        <div class="alert alert-danger">
                                            {{  $errors->first('question.iframe') }}
                                        </div>
                                    @endif
                                </div>

                                {{-- <div class="form-group d-flex flex-column">
                                    <label for="image">Imagen asociada</label>
                                    <input name="question[image]" type="file" class="py-2">
                                </div> --}}
                                <hr>

                                <legend>Respuestas</legend>
                                        <span id="choiceHelp" class="form-text text-muted">Por favor ingresa las posibles respuestas.</span>
                                        <br>

                                        <div class="form-group">

                                            <div class="form-group">
                                                <label for="option1" style="font-size: 17px;">Opcion A</label>
                                                <textarea name="answers[][description]" type="text" class="form-control" id="option1" aria-describedby="optionHelp" placeholder="Inserte la primera respuesta">{{ old('answers.0.description') }}</textarea>
                                            </div>

                                            @if($errors->has('answers.0.description'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.0.description') }}
                                                </div>
                                            @endif

                                           <div class="form-group">
                                                <label for="iframe">Video asociado *</label>
                                                <textarea name="answers[][iframe]" class="form-control" id="iframe" aria-describedby="iframeHelp" placeholder="Inserte la URL del video" >{{ old('answers.0.iframe') }}</textarea>
                                            </div>

                                            @if($errors->has('answers.0.iframe'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.0.iframe') }}
                                                </div>
                                            @endif

                                        {{--<div class="form-group">
                                                <input name="image[question]" type="file">

                                            </div> --}}
                                        </div> 

                                        <hr><br>

                                        <div class="form-group">
                                            <div class="form-group">
                                                <label for="option2" style="font-size: 17px;">Opcion B</label>
                                                <textarea name="answers[][description]" type="text" class="form-control" id="option2" aria-describedby="optionHelp" placeholder="Inserte la segunda respuesta" >{{ old('answers.1.description') }}</textarea>

                                            @if($errors->has('answers.1.description'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.1.description') }}
                                                </div>
                                            @endif
                                            </div>

                                            <div class="form-group">
                                                <label for="iframe">Video asociado *</label>
                                            <textarea name="answers[][iframe]" class="form-control" id="iframe" aria-describedby="iframeHelp" placeholder="Inserte la URL del video">{{ old('answers.1.iframe') }}</textarea>

                                            @if($errors->has('answers.1.description'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.1.iframe') }}
                                                </div>
                                            @endif
                                            </div>

                                        {{-- <div class="form-group">
                                                <input name="image[question]" type="file">

                                            </div>  --}}                                       
                                        </div>

                                        <hr><br>

                                        <div class="form-group">
                                            <div class="form-group">
                                                <label for="" style="font-size: 17px;">Opcion C</label>
                                                <textarea name="answers[][description]" type="text" class="form-control" id="option3" aria-describedby="optionHelp" placeholder="Inserte la tercera respuesta">{{ old('answers.2.description') }}</textarea>

                                            @if($errors->has('answers.2.description'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.2.description') }}
                                                </div>
                                            @endif
                                            </div>

                                            <div class="form-group">
                                                <label for="iframe">Video asociado *</label>
                                                <textarea name="answers[][iframe]" class="form-control" id="iframe" aria-describedby="iframeHelp" placeholder="Inserte la URL del video">{{ old('answers.2.iframe') }}</textarea>

                                            @if($errors->has('answers.2.iframe'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.2.iframe') }}
                                                </div>
                                            @endif
                                            </div>

                                    {{--    <div class="form-group">
                                                <input name="image[question]" type="file">
                                            </div> --}}
                                        </div>

                                        <hr><br>

                                        <div class="form-group">
                                            <div class="form-group">
                                                <label for="" style="font-size: 17px;">Opcion D</label>
                                                <textarea name="answers[][description]" type="text" class="form-control" id="option4" aria-describedby="optionHelp" placeholder="Inserte la cuarta respuesta">{{ old('answers.3.description') }}</textarea>

                                            @if($errors->has('answers.3.description'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.3.description') }}
                                                </div>
                                            @endif
                                            </div>

                                            <div class="form-group">
                                                <label for="iframe">Video asociado *</label>
                                                <textarea name="answers[][iframe]" class="form-control" id="iframe" aria-describedby="iframeHelp" placeholder="Inserte la URL del video">{{ old('answers.3.iframe') }}</textarea>

                                            @if($errors->has('answers.3.iframe'))
                                                <div class="alert alert-danger">
                                                    {{ $errors->first('answers.3.iframe') }}
                                                </div>
                                            @endif
                                            </div>

                                            {{--  <div class="form-group">
                                                <input name="image[question]" type="file">
                                            </div> --}}
                                        </div>
                                    <hr><br>
                                <button type="submit" class="btn btn-primary">Agregar pregunta</button>

                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
@endsection

Обновление Добавить dd ( $ data), когда я отправляю форму

array:4 [▼
  "_token" => "rpKkqpr9zmE7Q1ZN8L9CkQ42HJcDY4oi35aDhAqv"
  "user_id" => "1"
  "question" => array:2 [▼
    "description" => "https://youtu.be/ArMga9b77Fs"
    "iframe" => "https://youtu.be/y2TAeAzeqo4"
  ]
  "answers" => array:8 [▼
    0 => array:1 [▼
      "description" => "weuohqwepqjwe"
    ]
    1 => array:1 [▼
      "iframe" => "https://youtu.be/k7ORQhXN9rQ"
    ]
    2 => array:1 [▼
      "description" => "ouwhdouhqw"
    ]
    3 => array:1 [▼
      "iframe" => "https://youtu.be/1_nHJPGJvIo"
    ]
    4 => array:1 [▼
      "description" => "iuoashdahsoduasd"
    ]
    5 => array:1 [▼
      "iframe" => "https://youtu.be/k7ORQhXN9rQ"
    ]
    6 => array:1 [▼
      "description" => "wiodjoiashd"
    ]
    7 => array:1 [▼
      "iframe" => "https://youtu.be/k7ORQhXN9rQ"
    ]
  ]
]

и пи c.

введите здесь описание изображения

1 Ответ

1 голос
/ 20 июня 2020

проблема в вашем представлении: первый индекс массива ответов увеличивается при каждом вызове, поэтому у вас есть 8 значений для 4 вариантов.

попробуйте этот код для этого представления:

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <span>Crear nueva pregunta</span>
                        <a href="/exams/" class="btn btn-secondary btn-sm float-right" onclick="return confirm('Tus datos se perderan ¿Deseas regresar a la pagina principal?')">Regresar</a>
                    </div>
                        <div class="card-body">
                           @if($errors->any())
                                <div class="alert alert-danger">
                                    <h6>Por favor, verifica que los campos esten llenos</h6>
                                </div>
                            @endif

                            <form action="/exams/{{ $exam->id }}/questions" method="POST"  enctype="multipart/form-data">
                                @CSRF

                                <input hidden name="user_id" value="{{ auth()->user()->id }}">
                                <div class="form-group">

                                    <label for="description">Descripcion de la pregunta *</label>
                                    <textarea name="question[description]" type="text" class="form-control" id="description" aria-describedby="descriptionHelp" placeholder="Inserte la pregunta">{{ old('question.description') }}</textarea>
                                    <small id="descriptionHelp" class="form-text text-muted">Escribe la descripcion de la pregunta.</small>


                                    @if($errors->has('question.description'))
                                        <div class="alert alert-danger">
                                            {{ $errors->first('question.description') }}
                                        </div>
                                    @endif
                                </div>

                                <div class="form-group">
                                    <label for="iframe">Video asociado *</label>
                                    <textarea name="question[iframe]" type="text" class="form-control" id="iframe" aria-describedby="iframeHelp" placeholder="Inserte la URL del video">{{ old('question.iframe') }}</textarea>
                                    <small id="iframeHelp" class="form-text text-muted">Inserta la url del video.</small>

                                    @if($errors->has('question.iframe'))
                                        <div class="alert alert-danger">
                                            {{  $errors->first('question.iframe') }}
                                        </div>
                                    @endif
                                </div>

                                {{-- <div class="form-group d-flex flex-column">
                                    <label for="image">Imagen asociada</label>
                                    <input name="question[image]" type="file" class="py-2">
                                </div> --}}
                                <hr>

                                <legend>Respuestas</legend>
                                        <span id="choiceHelp" class="form-text text-muted">Por favor ingresa las posibles respuestas.</span>
                                        <br>
                                        @for ($i = 0, $j = 'A'; $i < 4; $i++, $j++)
                                            <div class="form-group">

                                                <div class="form-group">
                                                    <label for="option{{ $i }}" style="font-size: 17px;">Opcion {{ $j }}</label>
                                                    <textarea name="answers[$i][description]" type="text" class="form-control" id="option{{ $i }}" aria-describedby="optionHelp" placeholder="Inserte la primera respuesta">{{ old('answers.$i.description') }}</textarea>
                                                </div>

                                                @if($errors->has('answers.$i.description'))
                                                    <div class="alert alert-danger">
                                                        {{ $errors->first('answers.$i.description') }}
                                                    </div>
                                                @endif

                                               <div class="form-group">
                                                    <label for="iframe">Video asociado *</label>
                                                    <textarea name="answers[$i][iframe]" class="form-control" id="iframe" aria-describedby="iframeHelp" placeholder="Inserte la URL del video" >{{ old('answers.$i.iframe') }}</textarea>
                                                </div>

                                                @if($errors->has('answers.$i.iframe'))
                                                    <div class="alert alert-danger">
                                                        {{ $errors->first('answers.$i.iframe') }}
                                                    </div>
                                                @endif

                                            {{--<div class="form-group">
                                                    <input name="image[question]" type="file">

                                                </div> --}}
                                            </div> 

                                            <hr><br>
                                        @endfor

                                <button type="submit" class="btn btn-primary">Agregar pregunta</button>

                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
@endsection
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...