找不到 Laravel 模型关系类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29117969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel model relationships Class not found
提问by Loko
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_pagesand Groups. So in the models, I define the relationships like so:
所以我现在有 4 个模型:Users,Pages,Group_pages和Groups。所以在模型中,我定义了这样的关系:
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');
, but I dont know about the keys.
我不知道一对多的逆。我知道它是: return $this->belongsTo('table');
,但我不知道密钥。
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');
}
}
回答by Martin Bean
Model files should be named singularly and in camel-case, i.e. User
, Page
, Group
. A model representing the join between users and groups isn't necessary.
模型文件应单独命名并以驼峰命名,即User
, Page
, Group
. 表示用户和组之间的连接的模型不是必需的。
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. So your users
table should have a column named group_id
that's a foreign key referencing the id
column in your groups
table. Your relationship can then be expressed like this:
如果你按照 Laravel 的约定命名你的列,那么你也不需要在你的关系定义中指定它们。所以你的users
表应该有一个名为的列group_id
,它是一个引用表中id
列的外键groups
。你的关系可以这样表达:
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
回答by nozzleman
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)并改变对应关系(return $this->hasMany('GroupPage', 'group_id', 'id'); 在你的Group-model中。
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). The basic screencasts are all free (laravel 4 from scratchand laravel 5 fundamendals) and you will lern very fast in no time! Specifically, have a look at the episode on Eloquent Relationsships.
我总是喜欢向 Laravel 的新手推荐 Laracasts(我希望你还不知道)。基本的截屏视频都是免费的(从零开始的 laravel 4和laravel 5 基础知识),您很快就会学会!具体来说,看看Eloquent Relationsships上的那一集。
I also strongly recommend sticking to conventions
我也强烈建议遵守约定
- use the column-name
group_id
on theusers
-table for the group-foreign-key). - Classnames should be PascalCase ->
Group
, notgroup
, and when commiting them as parametes, stick to it (belongsTo('Group')
)...
- 使用-table
group_id
上的列名users
作为组外键)。 - 类名应该是 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。
回答by Jarek Tkaczyk
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