简体   繁体   中英

Laravel model relationships Class not found

In Laravel I just started with models and I have this database structure:

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 . 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

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.

Group_pages model:

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 . 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.

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:

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

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.

Then fix the relationship in your 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 scratch and laravel 5 fundamendals ) and you will lern very fast in no time! Specifically, have a look at the episode on 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).
  2. Classnames should be PascalCase -> Group , not group , and when commiting them as parametes, stick to it ( 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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