문제

User 모델 Organization 모델는 내가 하려고 관습니다.

users 테이블 idcurrent_organization_id (외국인 핵심)드(기타 일반 필드).

organizations 테이블 idowner_id (외국인 핵심)드(와 함께 다른 어떤 데이터가 분야).

또한 파일럿 테이블 organization_user 을 연결하는 두 개의를 통해 해당 id.

내 모델은 다음과 같이 설정하십시오.

용:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends \Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface {

  use UserTrait, RemindableTrait;

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';

  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
   */
  protected $hidden = array('password', 'remember_token');

  /**
   * Defining many to many relationship to organizations
   */
  public function organizations()
  {
    return $this->belongsToMany('Organization');
  }

  /**
   * Defining relationship to current selected organization
   */
  public function current_organization()
  {
    return $this->hasOne('Organization');
  }

}

조직:

<?php

class Organization extends \Eloquent {

    // Add your validation rules here
    public static $rules = [
        // 'title' => 'required'
    ];

    // Don't forget to fill this array
    protected $fillable = [];

  public function users()
  {
    return $this->hasMany('User');
  }

  public function owner()
  {
    return $this->belongsTo('User');
  }
}

아이디어는 하나의 조직에 의해 소유자이지만,조직에 많은 사용자,그리고 각 사용자는'현재'조직,그들이 선택할 수 있는 조직에서는 그들에 속한다.

는 문제로 실행하면도 $user->current_organization->idTrying to get property of non-object 오류,그리고 해보면 나 $user->current_organization()->id;Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$id 오류가 있습니다.

어떤 아이디어에서 제가 뭘 잘못하고 있는지 검색 current_organization 내가 하려고 하십니까?

편집:

나는 생각하고 그것은 나 hasOne 관계려고 했지만,이렇게:

public function current_organization()
{
  return $this->hasOne('Organization', 'id', 'current_organization_id');
}

그 역시 아무것도 보이지 않았습니다. $user->current_organization; 으로 돌아 NULL.

도움이 되었습니까?

해결책 2

후에는 많은 디버깅 및 검색에 걸쳐 온 나 이 게시물 는 나를 지도하여 실제 문제--밑줄에서 함수 이름입니다.

에서 링크:

웅변 문서 언급"하는 마음에서 방법을 따라야 낙타-케이스에도,데이터베이스의 열은 뱀 경우입니다." 그래서 나 믿는 함수 이름을해야한다"objectMinor()".웅변 실제로로 변환합니다 뱀의 경우 열 이름을 낙타-케이스 메서드 이름이 있습니다.이것은 비트이지만,그것은 그것을 따르 PHP 이름 지정 규칙을 따릅니다.

그래서 사용하여,@Cryode 에 대답하고,위에 나온:

public function currentOrganization()
{
  return $this->belongsTo('Organization', 'current_organization_id');
}

이라고 할 수있는을 통해:

$organization = $user->current_organization;

나는 생각한 그들은 정말이 더 명백하다.

다른 팁

당신이 사용하고 싶 belongsTo 에 대한 관계의 현재 조직입니다.

public function current_organization()
{
    return $this->belongsTo('Organization', 'current_organization_id');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top