dash
dash

Reputation: 387

Laravel Base table or view not found: 1146 Table

Im trying to get the 'diversities()' from items with a pivot table (belongToMany).

What am I missing?:

** Client works fine and Item and Client are identical (almost). The error I get is

Base table or view not found: 1146 Table 'restigo_spm.diversity_item' doesn't exist

and I dont have a diversity_item anywhere in the code, why is it searching for it?

Client:

class Client extends Model
{
    protected $fillable = ['name', 'diversity_id', 'type', 'enable'];

    public function diversities()
    {
        return $this->belongsToMany('App\Models\Diversity');
    }
}

ClientSchema:

Schema::create('clients', function (Blueprint $table) {
    $table->id();
    $table->unsignedInteger('client_diversity_id')->nullable();
    $table->string('name')->unique();
    $table->enum('type', ['restaurant', 'coffee', 'bar']);
    $table->boolean('enable');
    $table->timestamps();
});

ClientDiversity (pivot):

class ClientDiversity extends Model
{
    protected $table = 'client_diversity';
    protected $fillable = ['diversity_id', 'client_id'];
}

ClientDiversitySchema:

        Schema::create('client_diversity', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('client_id')->nullable();
            $table->unsignedInteger('diversity_id')->nullable();
            $table->timestamps();
        });

Item:

class Item extends Model
{
    protected $fillable = ['name', 'diversity_id', 'catalog_number', 'price', 'has_vat', 'enable'];

    public function diversities()
    {
        return $this->belongsToMany('App\Models\Diversity');
    }
}

ItemSchema:

        Schema::create('items', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('item_diversity_id')->nullable();
            $table->string('name');
            $table->string('catalog_number')->unique();
            $table->integer('price');
            $table->boolean('has_vat');
            $table->boolean('enable');
            $table->timestamps();
        });

Diversity:

class Diversity extends Model
{
    protected $fillable = ['name', 'item_id', 'client_id', 'enable'];

    public function clients()
    {
        return $this->belongsToMany('App\Models\Client');
    }

    public function items()
    {
        return $this->belongsToMany('App\Models\Item');
    }
}

DiversitySchmea:

        Schema::create('diversities', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->boolean('enable');
            $table->timestamps();
        });

Here is where I call it. The ClientControl code is exactly the same and works, but Item doesnt. ItemController:

class ItemController extends Controller
{
    public static function index()
    {
        $items = Item::with('diversities')->get();

        return new ItemCollection($items);
    }

Upvotes: 1

Views: 759

Answers (1)

Ersoy
Ersoy

Reputation: 9596

Because of the following method.

public function diversities()
{
    return $this->belongsToMany('App\Models\Diversity');
}

You are using belongsToMany which is many-to-many relationship. Since you didn't explicitly defined the table name for middle table, it creates it from the naming convention. It assumes that your middle table is the table of the first one diversity + _ and item.

You can use client_diversity as the second parameter to diversities method.

As it is stated in the documentation

As mentioned previously, to determine the table name of the relationship's joining table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method:

Upvotes: 1

Related Questions