php - Laravel 雄辩的关系错误 - 调用未定义的关系
问题描述
我有两张桌子schools
和subscriptions
. 我已经school_id
从我的subscriptions
桌子上拿到了。我的问题是当我想description
从表中获取列时schools
。我收到一个错误Call to undefined relationship [description] on model [App\Subscription].
有人可以告诉我我的代码有什么问题吗?我已经在我的模型和迁移中定义了关系。我不想使用 querybuilder 因为它太混乱了。谢谢。
学校模式
class School extends Model
{
protected $fillable = ['description'];
public function user(){
return $this->hasMany('App\User');
}
public function subscriber()
{
return $this->belongsTo('App\Subscription','description');
}
}
订阅模式
class Subscription extends Model
{
protected $fillable = [
'id', 'school_id', 'start_date', 'end_date',
];
public function subscriptions(){
return $this->hasMany('App\School','description');
}
public function plans(){
return $this->belongsTo('App\Plan');
}
}
我的控制器
$subs = Subscription::findOrFail($id)->with('description')
->get();
$plans = Plan::get();
dd($subs);
PS。当我只$subs = Subscription::findOrFail($id);
输入这些数据时dd
#attributes: array:8 [▼
"id" => 2
"school_id" => 2
"start_date" => "2019-09-27"
"end_date" => "2019-10-27"
"created_at" => "2019-10-17 03:36:13"
"updated_at" => "2019-10-17 03:36:13"
"plan_id" => 2
"status" => 1
]
学校表
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(191) | NO | UNI | NULL | |
| description | text | NO | | NULL | |
| logo | varchar(191) | YES | | NULL | |
| main_server | varchar(191) | YES | | NULL | |
| local_server | varchar(191) | YES | | NULL | |
| status | int(11) | NO | | 1 | |
| updated_by | varchar(191) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
订阅
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| school_id | int(10) unsigned | NO | MUL | NULL | |
| start_date | date | YES | | NULL | |
| end_date | date | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| plan_id | int(10) unsigned | NO | MUL | NULL | |
| status | int(11) | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
编辑:我只想得到我description
从中school_id
得到的$subs
解决方案
你打电话给错误的关系。你已经subscriptions
在description
你的Subscription Model
.
也许是错字?
您可以将您的关系更改为:
public function description(){
return $this->hasMany('App\School','id', 'school_id');
}
然后就这样称呼它:
$subs = Subscription::with('description')->findOrFail($id);
推荐阅读
- javascript - 具有相同 redux 反应组件的多个实例
- r - .R 文件的 MIME 类型
- mysql - Mysql 一对多聚合过滤器
- java - Recyclerview 没有显示图像
- java - 如何在java中随机化字符'+'和'-'
- r - R - 数据框 - 其他列中的每 x 行新数字
- python - 如何从图像模型中获取图像到 Django 的主页?
- javascript - Heroku + Node“错误:找不到模块'../config.js'需要堆栈:-/app/util/check-auth.js,-/app/graphql/resolvers/posts.js,-/app/index. js
- firebase - Firestore 集合链接
- c++ - 为什么静态变量没有被修改