Laravel 拉雷维尔一对多

Laravel 拉雷维尔一对多,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我有一个公司有地位的模型。这意味着有两个表: 坚定的 稳固状态 我在我的项目中定义如下: class FirmStatus extends Eloquent { protected $table = 'firm_statuses'; protected $primaryKey = 'firm_status_id'; protected $fillable = array('firm_status_id', 'name'); } class Firm exten

我有一个公司有地位的模型。这意味着有两个表:

  • 坚定的
  • 稳固状态
  • 我在我的项目中定义如下:

    class FirmStatus extends Eloquent {
    
        protected $table = 'firm_statuses';
    
        protected $primaryKey = 'firm_status_id';
    
        protected $fillable = array('firm_status_id', 'name');
    }
    
    
    
    class Firm extends Eloquent {
    
        protected $table = 'firms';
    
        protected $primaryKey = 'firm_id';
    
        protected $fillable = array('name', 'firm_status_id');
    
        public function users()
        {
            return $this->hasMany('User', 'firm_id', 'firm_id');
        }
    
        public function firm_status()
        {
            return $this->belongsTo('FirmStatus', 'firm_status_id');
        }
    }
    
    但我似乎无法让它发挥作用,无论我以何种方式尝试访问公司的FirmStatus,它就是不起作用

    在我的控制器中,我有以下代码:

            $firm = Firm::find($id);
        $firmClassifications = DB::table('firm_classifications')->orderBy('name', 'asc')->lists('name','firm_classification_id');
    
        return var_dump($firm);
    
    其中返回以下内容:

    object(Firm)[243]
      protected 'table' => string 'firms' (length=5)
      protected 'primaryKey' => string 'firm_id' (length=7)
      protected 'fillable' => 
        array (size=5)
          0 => string 'name' (length=4)
          1 => string 'firm_classification_id' (length=22)
          2 => string 'fca_number' (length=10)
          3 => string 'question_number_passed_on' (length=25)
          4 => string 'firm_status_id' (length=14)
      protected 'connection' => null
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=7)
          'firm_id' => int 8
          'name' => string 'siofds4' (length=7)
          'fca_number' => string '' (length=0)
          'firm_classification_id' => int 1
          'created_at' => string '2014-09-08 11:06:17' (length=19)
          'updated_at' => string '2014-09-08 12:24:23' (length=19)
          'firm_status_id' => int 2
      protected 'original' => 
        array (size=7)
          'firm_id' => int 8
          'name' => string 'siofds4' (length=7)
          'fca_number' => string '' (length=0)
          'firm_classification_id' => int 1
          'created_at' => string '2014-09-08 11:06:17' (length=19)
          'updated_at' => string '2014-09-08 12:24:23' (length=19)
          'firm_status_id' => int 2
      protected 'relations' => 
        array (size=0)
          empty
      protected 'hidden' => 
        array (size=0)
          empty
      protected 'visible' => 
        array (size=0)
          empty
      protected 'appends' => 
        array (size=0)
          empty
      protected 'guarded' => 
        array (size=1)
          0 => string '*' (length=1)
      protected 'dates' => 
        array (size=0)
          empty
      protected 'touches' => 
        array (size=0)
          empty
      protected 'observables' => 
        array (size=0)
          empty
      protected 'with' => 
        array (size=0)
          empty
      protected 'morphClass' => null
      public 'exists' => boolean true
    

    我希望$firm->firm_status(方法调用或不调用,无论哪种方式有效)将允许我访问指定的公司状态,但$firm->firm_status将导致空值。

    如果公司可以具有单个状态,请使用
    hasOne()
    而不是
    belongsTo()

    如果需要查询具有给定状态的所有公司,请将反向关系添加到
    FirmStatus

    class FirmStatus extends Eloquent {
        public function firm()
        {
            return $this->hasMany('Firm', 'firm_status_id', 'firm_status_id');
        }
    }
    

    首先,除非加载关系,否则在
    var\u dump($firm)
    result中不会看到任何内容

    下一件事-如果您想使用
    动态方法
    (调用
    $model->relationship
    ),Eloquent要求您对关系使用
    camelCased
    方法

    因此,您需要:

    public function firmStatus()
    {
        return $this->belongsTo('FirmStatus', 'firm_status_id');
    }
    
    然后您可以访问它:

    $firm->firmStatus; // FirmStatus model | null
    $firm->firm_status; // this will be translated to firmStatus anyway, but not other way around
    
    要在通话中查看,请执行以下操作:

    $firm = Firm::with('firmStatus')->first();
    var_dump($firm);
    // but better use toArray(), since it's easier to read
    dd($firm->toArray());
    

    注:主键不能由用户编辑;应该将其从FirmStatus
    filleble
    数组中删除,以避免意外行为。这一点很好!这将被更改。这仍然不起作用!我在模型的另一个部分中使用了它,但在这个例子中它不起作用。你能定义你看到的行为吗?你期望发生什么,而现在又发生了什么?您收到了什么错误消息?我没有收到错误消息。我在原始问题上添加了一些更详细的内容。您的模型正在识别公司状态关系,因为
    'firm\u status\u id'=>int 2
    。我注意到我错误地定义了反向关系:
    public function firm\u status()
    应该是
    public function firm()
    。你能告诉我当你做出改变时会发生什么吗?@GeorgeCummins你错了
    hasOne
    是另一种方式,您不能用
    belongsTo
    替换它。第一个需要在另一个模型上使用FK,而后一个需要在调用模型上使用FK。这就成功了!非常感谢。我想我必须使用坚定的身份,而不是坚定的身份。
    $firm = Firm::with('firmStatus')->first();
    var_dump($firm);
    // but better use toArray(), since it's easier to read
    dd($firm->toArray());