Я разрабатываю веб-сайт, который позволяет пользователям публиковать, просматривать и редактировать книги. В настоящее время я работаю над ролями каждого пользователя, и у меня возникла проблема с заполнением user_role. Я сталкиваюсь с ошибкой, приведенной ниже, но не могу понять, почему.
Единственное предположение, что он где-то называется role_user, но не могу понять, где и почему?
Illuminate\Database\QueryException : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dockerphp.role_user' doesn't exist (SQL: insert into `role_user` (`role_id`, `user_id`) values (1, 1))
at /srv/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
Exception trace:
1 PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dockerphp.role_user' doesn't exist")
/srv/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:457
2 PDO::prepare("insert into `role_user` (`role_id`, `user_id`) values (?, ?)")
/srv/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:457
My Сеялки выглядят так:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\User;
use App\Role;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::truncate();
DB::table('user_role')->truncate();
$authorRole = Role::where('name', 'Author')->first();
$editorRole = Role::where('name', 'Editor')->first();
$readerRole = Role::where('name', 'Reader')->first();
$Author = User::create([
'name' => 'Test Author',
'email' => 'Test@author.com',
'password' => Hash::make('testtest')
]);
$Editor = User::create([
'name' => 'Test Editor',
'email' => 'Test@editor.com',
'password' => Hash::make('testtest')
]);
$Reader = User::create([
'name' => 'Test Reader',
'email' => 'Test@reader.com',
'password' => Hash::make('testtest')
]);
$Author->roles()->attach($authorRole);
$Editor->roles()->attach($editorRole);
$Reader->roles()->attach($readerRole);
}
}
Похоже, что вышеупомянутая система столкнулась с проблемой, так как приведенная ниже сеялка работала отлично.
class RolesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Role::truncate();
Role::create(['name' => 'Author']);
Role::create(['name' => 'Editor']);
Role::create(['name' => 'Reader']);
}
}
Наконец, моя база данных выглядит так это:
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(RolesTableSeeder::class);
$this->call(UsersTableSeeder::class);
}
}
- Ролевая модель выглядит следующим образом:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users(){
return $this->belongsToMany('App\User');
}
}
И пользовательская модель
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'about', 'avatar'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function roles(){
return $this->belongsToMany('App\Role');
}
}