0

Trying to learn how to get a role name from the role table by linking it to the user table with role_id in user table and user_id in the role table.

I'm getting this error

Class 'App\Role' not found (View: C:\

All of my Role related files all reference role and files are names, RoleController and Role.php with a view called index.blade.php.

heres my role class:

<?php

namespace Laravel;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
//

protected $fillable = [
'name',
];

public function users()
{
return $this->belongsToMany('App\User');
}

}

Its been pointed out to me its probably a namespace issue but everyting else seems to point to Laravel like RoleController has:

namespace Laravel\Http\Controllers;

use Laravel\Role;
use Illuminate\Http\Request;

and user model has:

namespace Laravel;

So Why is this not working for me? as far as I can tell everything is named right.

1
  • 3
    use namespace App; Commented Mar 19, 2018 at 5:29

2 Answers 2

1

You should try this way:

Role.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
//

protected $fillable = [
'name',
];

public function users()
{
return $this->belongsToMany('App\User');
}

}

Use in Controller like:

use App\Role;
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to have worked for me, also added a pivot table.
@RathBaloth: Glad to help
0

The App namespace is used default by Laravel, cause it use the PSR-4 autoloading standard.

If you want to use your Laravel namespace, try to change your application name app:name by the artisan command like this php artisan app:name Laravel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.