複数のリレーションシップを持つ Laravel モデルが期待どおりに動作しない

StackOverflow https://stackoverflow.com//questions/25025878

質問

私は持っています User モデルと Organization 私が相互に関連付けようとしているモデル。

users テーブルには id そして current_organization_id (外部キー) フィールド (他の通常のフィールドの中でも)。

organizations テーブルには id そして owner_id (外部キー) フィールド (他のいくつかのデータ フィールドとともに)。

ピボットテーブルもあるので、 organization_user それぞれを介して 2 つを結び付けます 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');
  }
}

考え方としては、単一の組織は 1 人のユーザーによって所有されますが、組織には多数のユーザーが存在し、各ユーザーは「現在の」組織を持ち、自分が所属する任意の組織から選択できるということです。

私が直面している問題は、次のことをしようとしたときです $user->current_organization->id を取得します Trying 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()」であるべきだと思います。Eloquentは、実際にヘビケースの列名をメソッド名のキャメルケースに変換します。これは少し混乱していますが、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