drrhr20884 2014-07-29 22:14
浏览 35
已采纳

具有多种关系的Laravel模型未按预期运行

I have a User model and an Organization model which I'm trying to relate to one another.

The users table has id and current_organization_id (foreign key) fields (among the other normal fields).

The organizations table has id and owner_id (foreign key) fields (along with some other data fields).

There is also a pivot table, organization_user which links the two via their respective id.

My models are set up like this:

User:

<?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');
  }

}

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');
  }
}

The idea is that a single organization is owned by a user, but an organization has many users, and each user has a "current" organization, which they can select from any organization that they belong to.

The problem I'm running into is that when I try to do $user->current_organization->id I get a Trying to get property of non-object error, and if I try $user->current_organization()->id; I get a Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$id error.

Any ideas on what I'm doing wrong that I can't retrieve the current_organization like I'm trying to do above?

EDIT:

I'm thinking it has to do with my hasOne relationship, but I tried doing this:

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

and still nothing. $user->current_organization; is returning NULL.

  • 写回答

2条回答 默认 最新

  • dongyu7074 2014-07-31 14:24
    关注

    After much debugging and searching, I came across this post which led me to the real problem -- the underscore in the function name.

    From the link:

    The Eloquent docs mention "keep in mind that the methods should follow camel-casing, even though your database columns are snake-case." So I believe the function name should be "objectMinor()". Eloquent actually converts the snake-case column names to camel-case for the method names. This is a bit confusing, but it does it to conform to PHP naming conventions.

    So, using @Cryode answer and the above, I came up with:

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

    Which can be called via:

    $organization = $user->current_organization;
    

    I think they should really make this more obvious.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码