Rath Baloth
Rath Baloth

Reputation: 139

Laravel namespace class error

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.

Upvotes: 0

Views: 1163

Answers (2)

Mahefa
Mahefa

Reputation: 418

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

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

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;

Upvotes: 1

Related Questions