drtzb06222 2014-08-17 01:10
浏览 27
已采纳

Laravel两种模式之间的关系

I am struggling with working with relationships right now and would like some help as for how to make this relationship work. I am using Laravel.

Lets say you have a staff model that looks like so:

Staff.php

class Staff extends \Eloquent {
    protected $fillable = [];

    public function status()
    {
        return $this->belongsTo('Staff_status');
    }
}

The database table for the staff is as follows:

Table Name: staff
Fields: id, staffer_name, status_id

You also have a staff status model represented below:

Staff_statuses.php

class Staff_status extends Eloquent {

    public function staff()
    {
        return $this->hasMany('Staff');
    } 

}

You also have a staff database table like so:

Table Name: staff_statuses
Fields: id, status_name

However when I try and load the staff controller index method it says class Staff_status is not found.

Any idea why?

  • 写回答

2条回答 默认 最新

  • doubairan4213 2014-08-17 01:29
    关注

    You have used Staff_statuses.php as the name of your model but you are using Staff_status in the class name and thus you are calling it using Staff_status from your controller as well. This is the problem.

    Change the file name to match the class name. For example, use something like this:

    // StaffStatus.php
    class StaffStatus extends Eloquent{
    
        protected $table = 'staff_statuses';
    
        public function staff()
        {
            return $this->hasMany('Staff');
        } 
    }
    
    // Staff.php
    class Staff extends Eloquent {
    
        protected $table = 'staff';
    
        protected $fillable = [];
    
        public function status()
        {
            return $this->belongsTo('StaffStatus');
        }
    }
    

    In the controller you may use something like this:

    $staffStatus = StaffStatus::all();
    $staff = Staff::all();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误