首页 > 解决方案 > Laravel 雄辩的关系错误 - 调用未定义的关系

问题描述

我有两张桌子schoolssubscriptions. 我已经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

标签: phpmysqllaraveleloquent

解决方案


你打电话给错误的关系。你已经subscriptionsdescription你的Subscription Model.

也许是错字?

您可以将您的关系更改为:

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

然后就这样称呼它:

$subs = Subscription::with('description')->findOrFail($id);    

推荐阅读