Currently, I am trying to create roles for my application, unfortunately I am having some troubles. Whenever I run php artisan migrate --seed, I get the error I've written in the title. Honestly, I feel like I've messed up something really simple like a name but I just can't find my mistake. I'd appreciate any help.
User.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
public function roles(){
return $this->belongsToMany('App\Role');
}
}
Role.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users(){
return $this->belongsToMany('App\User');
}
}
Users table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('username');
$table->string('password');
$table->string('email');
$table->timestamps();
$table->rememberToken();
});
}
Roles table:
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description')->nullable()->default(null);
$table->timestamps();
});
}
role_user table
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('role_id');
$table->timestamps();
});
}
RoleTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Role;
class RoleTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$role_user = new Role();
$role_user->name = 'User';
$role_user->description = "Normal User";
$role_user->save();
$role_admin = new Role();
$role_admin->name = 'Admin';
$role_admin->description = "Admin User";
$role_admin->save();
}
}
UserTableSeeder.php
public function run()
{
$role_admin = Role::where('name', 'Admin')->first();
$user = new User();
$user->first_name = 'test';
$user->last_name = 'test';
$user->username = 'Admin';
$user->password = bcrypt('test');
$user->email = '[email protected]';
$user->save();
$user->roles()->attach($role_admin);
}
DatabaseSeeder.php
public function run()
{
$this->call(RoleTableSeeder::class);
$this->call(UserTableSeeder::class);
}
}
As mentioned in the comments:
Run: composer dump-autoload
.
If Composer\Exception\NoSslException
exception is thrown, you may need to run composer config -g -- disable-tls true
before composer dump-autoload
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With