【问题标题】:Problem with seeding data in pivot table in Laravel在 Laravel 的数据透视表中播种数据的问题
【发布时间】:2020-08-04 05:51:06
【问题描述】:

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

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

而我的表名实际上是 user_cuisine,所以好像由于某种原因 laravel 没有显示该表。由于字母顺序,我在我的关系中添加了 user_cuisine 是数据透视表。任何帮助表示赞赏。这是我的代码。

UserCuisineTableSeeder.php

<?php

use App\UserCuisine;
use App\UserProfile;
use Illuminate\Database\Seeder;

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 Illuminate\Database\Seeder;

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 Illuminate\Database\Eloquent\Model;

class UserCuisine extends Model
{
    protected $fillable = [
        'name'
    ];

    public function userProfiles()
    {
        return $this->belongsToMany(UserProfile::class, 'user_cuisine');
    }
}

用户配置文件.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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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');
    }
}

菜桌

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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');
    }
}

【问题讨论】:

    标签: php laravel laravel-seeding


    【解决方案1】:

    Laravel 期望表名是 user_cuisines,但您将其命名为 user_cuisine 而不带“s”

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

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class UserCuisine extends Model
    {
    
        protected $table = 'user_cuisine';
    
    }
    

    按照命名约定,我建议更改表名?

    【讨论】:

    • 好的,没错。但是现在我收到错误您请求了 6 个项目,但只有 0 个项目可用。
    • 你好@rose,你的播种机中的 random() 函数似乎是一个不同的问题。你确定你有美食吗? (您要求最多 12 个)
    • 是的,我看到了,但我无法解决它,我不知道为什么。你能帮忙吗?
    • @rose 而不是设置$cuisines-&gt;random(rand(1, 12))-&gt;pluck('id')-&gt;toArray() 你可以试试$cuisines-&gt;random(rand(1, $cuisine-&gt;count()))-&gt;pluck('id')-&gt;toArray()
    • 您可以运行一些故障排除并在您的UserCuisineTableSeeder 中使用dd($cuisines);。你确定你想要$cuisines = UserCuisine::all(); 而不是$cuisines = Cuisine::all(); 吗?
    【解决方案2】:

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

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

    protected $table= 'user_cuisine';
    

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

    php artisan make:model ModelName -m
    

    【讨论】:

    • 现在我得到错误 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_cuisine_id' in 'field list' (SQL: insert into user_cuisine (user_cuisine_id, user _profile_id ) 值 (2, 1), (8, 1), (10, 1))
    • 你好@rose 我已经更新了我错误的表名,你需要将值放入可填充数组中以插入记录
    • 在用户 UserCuisine 模型中的可填充数组应该是这样 protected $fillable = [ 'cuisine_id', 'user_id' ];
    • 这样做了,我收到了您请求了 1 个项目,但只有 0 个项目可用。看来它是随机播种机某处的错误,但我似乎找不到它。
    猜你喜欢
    • 2017-09-19
    • 2018-12-16
    • 2017-02-10
    • 2021-06-11
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2019-07-04
    • 2021-03-09
    相关资源
    最近更新 更多