提问者:小点点

找不到Laravel基表或视图:1146表


我试图从带有透视表(belongToMany)的项目中获取“diversities()”。

我错过了什么?:

**客户工作良好,项目与客户完全相同(几乎)。 我得到的错误是

找不到基表或视图:1146表'RESTIGO_SPM.DIVERSITY_ITEM'不存在

而且我在代码中的任何地方都没有diversity_item,为什么它要搜索它呢?

客户:

{
   protected $fillable = ['name', 'diversity_id', 'type', 'enable'];

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

客户端架构:

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

项目:

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

多样性:

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

我就这么叫它。 ClientControl代码完全相同,可以工作,但Item不能。 ItemController:

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

        return new ItemCollection($items);
    }

共2个答案

匿名用户

因为下面的方法。

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

您使用的是belongstomany,它是多对多关系。 由于您没有显式定义中间表的表名,因此它根据命名约定创建它。 它假设您的中间表是第一个diversity+_item的表。

如果您有多对多关系,那么使用ID(item_id,diversity_id)创建缺少表,ids是itemdiversity的外键。 如果没有多对多,则将其更改为belongsto,并重新排列关系上的键名。

匿名用户

你可以试试这个

class ClientDiversity extends Model{
    protected $table = 'client_diversity';
}

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