您知道为什么会出现“此集合实例上不存在“属性[registrationType]"的情况吗? [英] Do you know why it appears "Property [registrationType] does not exist on this collection instance"?

查看:131
本文介绍了您知道为什么会出现“此集合实例上不存在“属性[registrationType]"的情况吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道为什么"@if ($nextRegistration->participants->registration_type->contains('certificate_available', 'Y')) ["显示为"Property [registrationType] does not exist on this collection instance"吗?

Do you know why the " @if ($nextRegistration->participants->registration_type->contains('certificate_available', 'Y')) [ " shows "Property [registrationType] does not exist on this collection instance"?

如果"registration_types"表的"certificate_available"列的值为"Y",我想为每个注册显示一个链接获取证书".

I want to show for each registration a link "Get certificate" if the column "certificate_available" of the "registration_types" table has the value "Y".

<ul class="list-group">
@foreach($nextRegistrations as $nextRegistration)
    @if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
            @if ($nextRegistration->participants->registration_type->contains('certificate_available', 'Y'))                                                 
            <a href="{{route('conferences.certificateInfo',
            [
            'regID'=> $nextRegistration->id])}}"
                   class="btn btn-primary ml-2">Download certificate</a>
            @endif
        </li>
    @endif
@endforeach
</ul>

参与者模型具有registration_type():

The participant model has the registration_type():

class Participant extends Model
{


    public function registration(){
        return $this->belongsTo('App\Registration');
    }


    public function registration_type(){
        return $this->belongsTo('App\RegistrationType');
    }

}

要获取$ nextRegistrations的代码是:

To get the $nextRegistrations the code is:

$nextRegistrations = $user->registrations()
            ->with('participants.registration_type')
            ->whereHas(
                'conference',
                function ($query) {
                    $query->where('end_date', '>', now());
                }
            )->paginate($pageLimit);

$ nextRegistrations显示如下:

And $nextRegistrations shows like:

LengthAwarePaginator {#336 ▼
  #total: 3
  #lastPage: 1
  #items: Collection {#320 ▼
    #items: array:3 [▼
      0 => Registration {#308 ▼
        ...
        #relations: array:1 [▼
          "participants" => Collection {#327 ▼
            #items: array:1 [▼
              0 => Participant {#332 ▼
               ...
                #relations: array:1 [▼
                  "registration_type" => RegistrationType {#337 ▼
                    #fillable: array:7 [▶]
                   ....
                    #attributes: array:10 [▼
                      "id" => 1
                      "name" => "general"
                      "description" => "desc general"
                      "conference_id" => 1
                      "certificate_id" => 1
                      "certificate_available" => "Y"
                    ]
                    ...
                  }
                ]
                ...
              }
            ]
          }
        ]
        ...
      }
      1 => Registration {#321 ▶}
      2 => Registration {#317 ▶}
    ]
  }
  ....
}

推荐答案

如何从数据库中检索数据.您可能需要使用with方法:

How do you retrieve the data from the database. You may need to use the with method:

$data = Participant::with('registration_type')->get()

或在视图中使用"registration_type"作为方法,而不是属性:

or in the view use "registration_type" as a method, not as a property:

$nextRegistration->participants->registration_type()

您可以在此处找到有关归属关系的更多信息: https://laravel.com/docs/5.6/eloquent-relationships#one-to-many-inverse

You can find more about the belongs to relationship here: https://laravel.com/docs/5.6/eloquent-relationships#one-to-many-inverse

这篇关于您知道为什么会出现“此集合实例上不存在“属性[registrationType]"的情况吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆