Edit:
I dont think its the same issue as:
https://stackoverflow.com/questions/40022929/laravel-relationship-error-undefined-property-illuminate-database-eloquent-col
because in that issue hasMany relationship is used which returns an array but i used belongsTo which should return a certain object.
저는 사용자와 회사 테이블간에 많은 관계가 많은 데이터베이스 구조를 가지고 있습니다. 이를 위해 나는 crossreference table company_user를 가지고있다. 또한 각 사용자는 회사에서 특정 역할을 갖기 때문에 상호 참조 테이블에도 role_id가 있습니다. 문제는 어떤 이유로 crossreference 테이블에서 역할을 검색하려고 할 때 예외가 발생한다는 것입니다. 난 그냥 피벗 모두에서 ROLE_ID를 얻을하려고하면 잘 작동 이제사용자 정의 피벗 모델에서 Laravel - belongsTo 관계가 작동하지 않습니다.
public function users() {
return $this->belongsToMany('App\User')->withPivot('role_id')->using('App\CompanyUser');
}
:
이@foreach($company->users as $user)
{{$user->pivot->role_id}} // this displays correct role_id
@endforeach
그러나 나는 또한의 데이터가 필요 여기
내가 회사 모델의 관계를 정의하는 방법이다 역할 그래서 내 정의 피벗에서 관계를 정의. 여기에 전체 모델 :
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class CompanyUser extends Pivot
{
public function role()
{
return $this->belongsTo('App\Role');
}
}
그리고 난 이런 식으로 접근을 시도 :
@foreach($company->users as $user)
{{$user->pivot->role()->id}}
@endforeach
그러나 이것은 나에게 예외 제공 :
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$id
내가 놓치고 무엇을?
가능한 복제로 변경하려고 [Laravel 관계 오류 : 정의되지 않은 속성을 : 웅변 \ 컬렉션 \ \ 데이터베이스를 조명 : $ id를 라인 1] (https://stackoverflow.com/questions/ 40022929/laravel-relationship-error-undefined-property-illuminate-database-eloquent-col) –