Laravel中数据透视表中的种子设定数据存在问题



我在Laravel中的数据库中尝试种子数据透视表时遇到问题。我得到错误

Base table or view not found: 1146 Table 'user_cuisines' doesn't exist

我的表名实际上是user_cuisine,所以出于某种原因,laravel没有显示那个表。我在我的关系中添加了user_cuisine是透视表,因为字母顺序。感谢您的帮助。这是我的密码。

UserCuisineTableSeeder.php

<?php
use AppUserCuisine;
use AppUserProfile;
use IlluminateDatabaseSeeder;
class UserCuisineTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$cuisines = UserCuisine::all();
UserProfile::all()->each(function ($userProfile) use ($cuisines) { 
$userProfile->cuisines()->attach(
$cuisines->random(rand(1, 12))->pluck('id')->toArray()
); 
});
}
}

DatabaseSeeder.php

<?php
use IlluminateDatabaseSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(WhitelabelTableSeeder::class);
$this->call(CitiesTableSeeder::class);
$this->call(UserTableSeeder::class);
$this->call(UserProfilePropertiesSeeder::class);
$this->call(UserProfileTableSeeder::class);
$this->call(FieldTableSeeder::class);
$this->call(FieldValueTableSeeder::class);
$this->call(CuisineTableSeeder::class);
$this->call(LiteratureTableSeeder::class);
$this->call(MusicTableSeeder::class);
$this->call(SportTableSeeder::class);
$this->call(TvTableSeeder::class);
$this->call(UserCuisineTableSeeder::class);
$this->call(UserInterestTableSeeder::class);
}
}

UserCuisine.php

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class UserCuisine extends Model
{
protected $fillable = [
'name'
];
public function userProfiles()
{
return $this->belongsToMany(UserProfile::class, 'user_cuisine');
}
}

UserProfile.php

<?php
namespace App;
class UserProfile
{
public function user()
{
return $this->belongsTo(User::class);
}
public function cuisines()
{
return $this->belongsToMany(UserCuisine::class, 'user_cuisine');
}
}

user_cuisine_table

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateUserCuisineTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_cuisine', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('cuisine_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_cuisine');
}
}

cuisine_table

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateCuisineTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cuisine', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cuisine');
}
}

Laravel期望表名为user_cuisines,但您将其命名为user_cuisine而不使用"s">

您可以通过更改迁移或将protected $table = 'user_cuisine';添加到模型中来解决此问题。

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class UserCuisine extends Model
{
protected $table = 'user_cuisine';
}

按照命名惯例,我建议更改表名

似乎user_cuisines表不存在,您创建的是cuisine而不是user_cuisines

只需将以下代码放在UserCuisine模型中即可。

protected $table= 'user_cuisine';

例如,我建议在创建模型时始终使用-m标志,以避免这些类型的错误。

php artisan make:model ModelName -m

最新更新