简体   繁体   English

Laravel模型关系未找到类

[英]Laravel model relationships Class not found

In Laravel I just started with models and I have this database structure: 在Laravel我刚开始使用模型,我有这个数据库结构:

users

id | username | password | group | //(group is the id of the group)


groups

id | groupname |


group_pages

group_id | page_id | create | read | update | delete


pages

id | pagename

I am trying to check if the user can create/read/update/delete on the page he's on. 我试图检查用户是否可以在他所在的页面上创建/读取/更新/删除。

So I have 4 Models for this at the moment: Users , Pages , Group_pages and Groups . 所以我现在有4个模型: UsersPagesGroup_pagesGroups So in the models, I define the relationships like so: 所以在模型中,我定义了这样的关系:

User model: 用户模型:

public function group()
{
    return $this->belongsTo('group', 'group', 'id');
}

Group Model: 团体模型:

public function users()
{
    return $this->hasMany('users', 'group', 'id');
}

public function group_pages()
{
    return $this->hasMany('group_pages', 'group_id', 'id');
}

I am using this in my controller like this: 我在我的控制器中使用这个:

$group_id = User::find(Session::get('user_id'));
$crud = Group::find($group_id->group)->group_pages()->first();

As described in the documentation . 文档所述

but this is giving me the error: 但这给了我错误:

Class group_pages not found 找不到类group_pages

What is going wrong here? 这里出了什么问题?

I'm not sure about assigning the keys in the relationships. 我不确定在关系中分配键。

I know this: 我知道这个:

One to One Inverse: 一对一反转:

return $this->belongsTo('class', 'local_key', 'parent_key');

One to Many: 一对多:

return $this->hasMany('class', 'foreign_key', 'local_key'); 

I dont know about the One to Many Inverse . 我不知道一对多逆 I know it's: return $this->belongsTo('table'); 我知道: return $this->belongsTo('table'); , but I dont know about the keys. ,但我不知道钥匙。

Group_pages model: Group_pages模型:

class Group_pages extends Eloquent {

    public function pages()
    {
        return $this->belongsTo('pages', 'id', 'group_id');
    }

    public function group()
    {
        return $this->belongsTo('group', 'id', 'group_id');
    }

}

Model files should be named singularly and in camel-case, ie User , Page , Group . 模型文件应单独命名并以驼峰形式命名,即UserPageGroup A model representing the join between users and groups isn't necessary. 表示用户和组之间的连接的模型不是必需的。

Then when it comes to defining the relationships, the first parameter is the class name of the model: 然后,在定义关系时,第一个参数是模型的类名:

class User {

    public function group()
    {
        return $this->belongsTo('Group', 'local_key', 'parent_key');
    }
}

You're making life difficult for yourself by going against Laravel's conventions. 你违背了Laravel的惯例,让自己的生活变得困难。

If you name your columns as per Laravel's conventions, you then don't need to specify them in your relationship definitions either. 如果按照Laravel的约定命名列,则无需在关系定义中指定它们。 So your users table should have a column named group_id that's a foreign key referencing the id column in your groups table. 因此,您的users表应该有一个名为group_id的列,该列是引用groups表中id列的外键。 Your relationship can then be expressed like this: 然后你的关系可以这样表达:

class User {

    public function group()
    {
        return $this->belongsTo('Group');
    }
}

A lot more succinct and easier to read, and you don't have to remember which way around the local and foreign column names go. 更简洁,更易于阅读,您不必记住本地和外部列名称的方式。

You can read more about the conventions Laravel uses for model and relation names in the official documentation: http://laravel.com/docs/master/eloquent#relationships 您可以在官方文档中阅读有关Laravel用于模型和关系名称的约定的更多信息: http ://laravel.com/docs/master/eloquent#relationships

You defined your relationship with a model-class that does not exists. 您使用不存在的模型类定义了您的关系。

To solve this, create a group_page-model (or even better GroupPage) and change the corresponding relationship (return $this->hasMany('GroupPage', 'group_id', 'id'); within your Group-model. 要解决此问题,请创建group_page-model(或更好的GroupPage)并更改相应的关系(在Group-model中返回$ this-> hasMany('GroupPage','group_id','id');

Then fix the relationship in your User -model: 然后在User -model中修复关系:

public function group() // typo! not groep..
{
    return $this->belongsTo('group', 'group'); // remove id, you do not need it
}

Then there is a problem with your controller code which might be fixable like that: 然后你的控制器代码有一个问题,可能是这样的:

$group_id = User::find(Session::get('user_id'))->group()->id;
$crud = Group::find($group_id)->group_pages()->first();

I always like to recommend Laracasts to peopel who are new to Laravel (i hope you do not know this yet). 我总是喜欢向Laravel推荐Laracasts,他们是Laravel的新手(我希望你还不知道)。 The basic screencasts are all free ( laravel 4 from scratch and laravel 5 fundamendals ) and you will lern very fast in no time! 基本的截屏视频都是免费的( laravel 4 from scratch and laravel 5 fundamendals ),你很快就能快速出镜! Specifically, have a look at the episode on Eloquent Relationsships . 具体来说,看一下关于Eloquent Relationsships的剧集。

I also strongly recommend sticking to conventions 我也强烈建议坚持惯例

  1. use the column-name group_id on the users -table for the group-foreign-key). 在group-foreign-key的users group_id上使用column-name group_id
  2. Classnames should be PascalCase -> Group , not group , and when commiting them as parametes, stick to it ( belongsTo('Group') )... 类名应该是PascalCase - > Group ,而不是group ,当把它们作为参数提交时,坚持它( belongsTo('Group') )...

This makes life much easier! 这让生活更轻松!

Finally 最后

Be aware that there might be packages for what you are trying to achieve. 请注意,您可能会尝试使用软件包。 One that comes to my mind is Entrust. 我想到的是Entrust。

You're making your life hard with this code and thus you can't make it work. 你用这个代码让你的生活变得艰难,因此你无法使它生效。

Check this out first: 首先检查一下:

// This is User model, NOT group_id
$group_id = User::find(Session::get('user_id'));

Next: 下一个:

public function group() // I suppose groep was typo, right?
{
    // having relation with same name as the column
    // makes Eloquent unable to use the relation in fact
    return $this->belongsTo('group', 'group', 'id');
}

So, here's what you need: 那么,这就是你需要的:

// User model
public function group()
{
    return $this->belongsTo('Group', 'group_id'); // rename column to group_id
}

// Group model
public function pages()
{
    return $this->belongsToMany('Page', 'group_pages')
        ->withPivot(['create', 'read', 'update', 'delete']);
}

Then: 然后:

$user = User::find(Session::get('user_id')); // or use Auth for this
$page = $user->group->pages()->find($currentPageId);
// now you can access pivot fields:
$page->pivot->create;
$page->pivot->update;
... and so on

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM