Знаете ли вы, почему $ customQuestion-> required равен нулю? - PullRequest
0 голосов
/ 24 апреля 2018

У меня есть форма для ввода пользователем информации для регистрации в конгрессе.

В форме есть эта часть для отображения обязательного атрибута, только если в пользовательском вопросе есть столбец «обязательный» со значением «1»:

@foreach($selectedType['questions'] as $customQuestion)

    <div class="form-group">
        <label for="participant_question">{{$customQuestion->question}}</label>
        <p>REQUIRED VALUE:::::: {{$customQuestion->required }}</p>
        <input type="text" @if($customQuestion->required == "1") required @endif class="form-control" name="participant_question[]" value="">

    </div>
@endforeach

Но это не работает, потому что "{{$customQuestion->required }}" в "<p>REQUIRED VALUE:::::: {{$customQuestion->required }}</p>" равно null, ничего не показывает.

Ты знаешь почему?

"$selectedType['questions']" происходит из этого метода в RegistrationController:

public function storeQuantities(Request $request, $id, $slug = null){

        $ttypeQuantities = $request->get('ttypes');

        $all_participants = Congress::where('id', $id)->first()->all_participants;

        foreach($ttypeQuantities as $ttypeName => $quantity){
            if($quantity) {
                $ttype = TicketType::where('name', $ttypeName)->firstOrFail();
                $price = $ttype->price;

                $selectedType[$ttype->name]['quantity'] = $quantity;
                $selectedType[$ttype->name]['price'] = $price;
                $selectedType[$ttype->name]['subtotal'] = $price * $quantity;
                $selectedType[$ttype->name]['questions'] = $ttype->questions;

            }
        }
        Session::put('selectedTypes', $selectedTypes);
        Session::put('all_participants' , $all_participants);
        Session::put('customQuestions' ,  $selectedTypes[$ttype->name]['questions']);
        //dd($selectedTypes);
        return redirect(route('congresses.registration',['id' => $id, 'slug' => $slug]));
    }

Таблица взаимосвязей релевантных для вопроса:

1 to many between congress and ticket types (a congress can have many ticket types)
1 to many between ticket types and ticket_type_questions (a ticket type can have many custom questions)
1 to many between questions and ticket_type_questions (a question can be associated with many ticket types)

Таблица ticket_type_questions имеет следующую структуру: id, ticket_type_id, question_id, required. Обязательный столбец равен 1, если пользовательский вопрос требуется для этого типа заявки, и 0, если не требуется.

Модели, имеющие отношение к вопросу:

// Congress model
class Congress extends Model
{

    // A congress has many ticket types
    public function ticketTypes(){
        return $this->hasMany('App\TicketType', 'congress_id');
    }
}

// TicketType Model

class TicketType extends Model
{
    public function congress(){
        return $this->belongsTo('App\Congress');
    }

    public function questions(){
        return $this->belongsToMany('App\Question', 'ticket_type_questions');
    }
}

// TicketTypeQuestion model

class TicketTypeQuestion extends Model
{

}


class Question extends Model
{

    public function ticket_type(){
        return $this->belongsToMany('App\TicketType', 'ticket_type_questions')
            ->withPivot('required');
    }
}

1 Ответ

0 голосов
/ 24 апреля 2018

попробуйте $customQuestion->ticket_type->required

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