Heimdall
2 weeks ago
210
13
General

Has many relation

Posted 2 weeks ago by Heimdall

Hello,

I have 3 table ( dish / category and pivot table category_dish )

One dish can be to belong at many category.

So on my pivot table i save the id_category and id_dish

But when i want display the good dish on the good category i have error...

My code :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Dishs extends Model
{
        public function Category()
    {
          return $this->belongsToMany('App\Category', 'category_dish');
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function Dishs()
    {
        return $this->HasMany('App\Dishs', 'category_dish');
    }
}

My controller :

$dish_by_category = Dishs::with('category')->get();

and i have this error :

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'restaurant.category_dish' doesn't exist (SQL: select categories.*, category_dish.dishs_id as pivot_dishs_id, category_dish.category_id as pivot_category_id from categories inner join category_dish on categories.id = category_dish.category_id where category_dish.dishs_id in (5, 6, 7, 8, 9, 10, 11, 12))

Ty for your help

Please sign in or create an account to participate in this conversation.