从数据透视表获取值无法正常工作

时间:2018-04-24 16:36:41

标签: 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>
        <p>REQUIRED VALUE:::::: {{$customQuestion->ticket_type->pivot->required}}</p>
         <p>REQUIRED VALUE:::::: {{$customQuestion}}</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; {{$customQuestion}}&#34;所示:

{"id":1,
 "question":"Whats your phone?",
 "type":"text","congress_id":1,
 "pivot":{"ticket_type_id":1,
 "question_id":1},
 "ticket_type":[{"id":1,
 "name":"test",
 "congress_id":1,
 "pivot":{"question_id":1,
 "ticket_type_id":1,"required":1}}]}

&#34; {{$customQuestion->ticket_type->pivot->required}}&#34;显示:&#34; Property [pivot] does not exist on this collection instance.&#34;。

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

模型的相关性:

// 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 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。

&#34; $selectedTypes&#34;来自RegistrationController storeUserInfo()方法:

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 个答案:

答案 0 :(得分:0)

您需要在->withPivot(..) TicketType关系中添加questions()

class TicketType extends Model
{
    public function questions(){
        return $this->belongsToMany('App\Question', 'ticket_type_questions')
            ->withPivot(['required']);
    }
}

然后你可以这样做:

{{ $customQuestion->pivot->required }}