你知道为什么$ customQuestion-> required是null吗?

时间:2018-04-24 13:49:06

标签: laravel

我有一个表格供用户输入一些信息在国会注册。

在表单中,只有当自定义问题具有列" required"时才会显示此部分。值为" 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

但它不起作用因为&#34; {{$customQuestion->required }}&#34; in&#34; <p>REQUIRED VALUE:::::: {{$customQuestion->required }}</p>&#34;是null,不要显示任何内容。

你知道为什么吗?

&#34; $selectedType['questions']&#34;来自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 :(得分:0)

试试这个[ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty-one", "twenty-two", "twenty-three", "twenty-four", "twenty-five", "twenty-six", "twenty-seven", "twenty-eight", "twenty-nine", "thirty" ]