为什么没有显示参与者的姓名?

时间:2018-07-01 23:04:21

标签: php laravel

我在getRegistrationInfo()中有一个查询以获取会议中注册的详细信息,因此可以显示与该注册关联的每个参与者的列表项。

例如,如果用户John在会议“测试会议”中注册了他和他的同事Jake,则在注册类型“一般”中我都想显示两个列表项,例如:

Conference Name: test conference
Registration Type: general
Participant: Jake

Conference Name: test conference
Registration Type: general
Participant: John

它适用于会议名称和注册类型名称,但是与会人员名称不起作用,显示为“试图获取非对象的属性”和“ {{$registrationType->participants->participant->name}}”。

您知道问题出在哪里吗?

getRegistrationInfo()方法:

 public function getRegistrationInfo($regID)
    {
         $registration = Registration::with('conference.registrationTypes.participants')
        ->find($regID);

        $pdf = PDF::loadView('pdf.registration', compact('registration'));
        return $pdf->download('info.pdf');
    }

然后在视图中:

@foreach($registration->conference->registrationTypes as $registrationType)
    <li>
        <div class="wrapper">
            <div class="conference-title">
                <strong>Conference</strong><br> <span>{{$registration->conference->name}}</span>
            </div>
            <div class="conference-regtype">
                <div>
                    <strong>Registration Type </strong><br>
                    <span>{{$registrationType->name}}</span>
                </div>
            </div>

            <div class="conference-participant">
                <strong>Participant</strong><br>
                <span>{{$registrationType->participants->participant->name}}</span>
            </div>
        </div>
    </li>
@endforeach

getregationInfo()方法中$ registration的输出:

Registration {#313 ▼
  #relations: array:1 [▼
    "conference" => Conference {#318 ▼
      #relations: array:1 [▼
        "registrationTypes" => Collection {#319 ▼
          #items: array:1 [▼
            0 => RegistrationType {#324 ▼
              #relations: array:1 [▼
                "participants" => Collection {#325 ▼
                  #items: array:2 [▼
                    0 => Participant {#331 ▼
                      #relations: []
                    }
                    1 => Participant {#333 ▼
                      #relations: []
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

$ registrationType的输出:

RegistrationType {#324 ▼
  #relations: array:1 [▼
    "participants" => Collection {#325 ▼
      #items: array:2 [▼
        0 => Participant {#331 ▼
          #fillable: array:4 [▼
            0 => "name"
            1 => "surname"
            2 => "registration_id"
            3 => "registration_type_id"
          ]
        }
        1 => Participant {#333 ▼
          #fillable: array:4 [▼
            0 => "name"
            1 => "surname"
            2 => "registration_id"
            3 => "registration_type_id"
          ]
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

您的输出应如下:

{
  "Place": "{0}",
  "SkuNumber": "SKU_{1}"
}

您可能必须创建或调整与操作中涉及的模型的关系。