Laravel中的一对多

时间:2014-09-08 13:41:25

标签: laravel laravel-4 eloquent

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

  1. 事务所
  2. FirmStatus
  3. 我在我的项目中定义了这个:

    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结果为null。

2 个答案:

答案 0 :(得分:0)

如果公司可以拥有单一状态,请使用hasOne()代替belongsTo()

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

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

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

答案 1 :(得分:0)

首先,除非你加载关系,否则你不会在var_dump($firm)结果中看到任何内容。

接下来 - 如果您想使用camelCased(来电dynamic methods),则Eloquent要求您使用$model->relation方法进行关系。

所以你需要:

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());