2

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');
    }
}
rose
  • 447
  • 5
  • 18

2 Answers2

2

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

Christophe Hubert
  • 2,833
  • 1
  • 12
  • 25
  • Okay, that is correct. But now I get error You requested 6 items, but there are only 0 items available. – rose Apr 21 '20 at 13:13
  • hello @rose, seems like a different issue on your random() function in your Seeder. Are you sure you have cuisines ? (you are asking for up to 12) – Christophe Hubert Apr 21 '20 at 13:21
  • Yes I see that, but I just can't resolve it and I don't know why. Can you help? – rose Apr 21 '20 at 13:24
  • And yes I have, cuisines table populated from id 1 to 12 – rose Apr 21 '20 at 13:27
  • 1
    @rose instead of setting `$cuisines->random(rand(1, 12))->pluck('id')->toArray()` you can try with `$cuisines->random(rand(1, $cuisine->count()))->pluck('id')->toArray()` – Christophe Hubert Apr 21 '20 at 13:30
  • You requested 1 items, but there are only 0 items available. again similar error – rose Apr 21 '20 at 13:35
  • 1
    You could run some troubleshooting and use `dd($cuisines);` in your `UserCuisineTableSeeder`. As well are you sure you want `$cuisines = UserCuisine::all();` and not `$cuisines = Cuisine::all();` ? – Christophe Hubert Apr 21 '20 at 13:40
  • $cuisines returns empty array when I dump it under $cuisines = UserCuisine::all(); – rose Apr 21 '20 at 13:45
  • Yeah I got it.It works now. I changed protected $table = 'cuisine'; so now it returns full array. Anyway you helped me, thanks! – rose Apr 21 '20 at 13:48
  • alrighty, don't forget to approve the answer if you are satisfied – Christophe Hubert Apr 21 '20 at 13:49
1

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
Rahul
  • 1,617
  • 1
  • 9
  • 18
  • With this now I get error 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`) values (2, 1), (8, 1), (10, 1)) – rose Apr 21 '20 at 13:14
  • Hello @rose I have updated the table name that was my mistake, You need to put value in the fillable array to insert the records – Rahul Apr 21 '20 at 13:29
  • 1
    In the user UserCuisine model fillable array shuld be like this protected $fillable = [ 'cuisine_id', 'user_id' ]; – Rahul Apr 21 '20 at 13:30
  • Did that, and I get You requested 1 items, but there are only 0 items available. it appears it is error somewhere in seeder around random but I cant seem to find it. – rose Apr 21 '20 at 13:36