gpt4 book ai didi

php - Laravel Eloquent 关系错误 - 调用未定义的关系

转载 作者:行者123 更新时间:2023-11-29 06:27:55 27 4
gpt4 key购买 nike

我有两个表schoolssubscriptions。我已经从我的 subscriptions 表中获取了 school_id。我的问题是当我想从 schools 表中获取 description 列时。我收到错误调用模型 [App\Subscription] 上未定义的关系 [description]。有人能告诉我我的代码有什么问题吗?我已经在模型和迁移中定义了关系。我不想使用查询构建器,因为它太困惑了。谢谢。

学校模型


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

我的 Controller

 $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 | |
+------------+------------------+------+-----+---------+----------------+

编辑:我只想从 school_id 获取我从 $subs 获取的 description

最佳答案

在订阅模型中更改关系函数订阅

public function subscriptions(){
return $this->hasMany('App\School', 'id', 'school_id');
}

调用 Eloquent 查询

$subs = Subscription::with('subscriptions')->findOrFail($id);
dd($subs->subscriptions[0]->description);

关于php - Laravel Eloquent 关系错误 - 调用未定义的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com