rose
rose

Reputation: 197

Problem with seeding data in pivot table in Laravel

I have a problem while attempting to seed my pivot table in database in Laravel. I get error

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

And my table name is actually user_cuisine, so it is like for some reason laravel doesn't show that table. I added in my relationships that user_cuisine is pivot table because of alphabet order. Any help is appreciated. Here is my code.

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

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

cuisine_table

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

Upvotes: 1

Views: 890

Answers (2)

Rahul
Rahul

Reputation: 1615

Seems user_cuisines table does not exist, You have created cuisine not user_cuisines

Just put below code in UserCuisine model.

protected $table= 'user_cuisine';

I would recommend always use -m flag while creating a model to avoid these types of errors, for example.

php artisan make:model ModelName -m

Upvotes: 1

Christophe Hubert
Christophe Hubert

Reputation: 2951

Laravel expect the table name to be user_cuisines but you named it user_cuisine without the "s"

You can fix that by changing your migration or adding protected $table = 'user_cuisine'; to your model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserCuisine extends Model
{

    protected $table = 'user_cuisine';

}

As naming convention goes, I would recommend changing the table name 👍

Upvotes: 2

Related Questions