Laravel 未找到基础 table 或视图:1146 Table
Laravel Base table or view not found: 1146 Table
我正在尝试从具有枢轴 table (belongToMany) 的项目中获取 'diversities()'。
我错过了什么?:
** Client 工作正常并且 Item 和 Client 完全相同(几乎)。
我得到的错误是
未找到基础 table 或视图:1146 Table 'restigo_spm.diversity_item' 不存在
而且我在代码中的任何地方都没有 diversity_item,为什么要搜索它?
客户:
class Client extends Model
{
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(枢轴):
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');
}
}
项目架构:
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 不行。
项目控制器:
class ItemController extends Controller
{
public static function index()
{
$items = Item::with('diversities')->get();
return new ItemCollection($items);
}
因为下面的方法。
public function diversities()
{
return $this->belongsToMany('App\Models\Diversity');
}
您正在使用 belongsToMany
,这是多对多关系。由于您没有明确定义中间 table 的 table 名称,它根据命名约定创建它。它假定您的中间 table 是第一个 diversity
+ _
和 item
.
的 table
您可以使用 client_diversity
作为 diversities
方法的第二个参数。
中所述
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:
我正在尝试从具有枢轴 table (belongToMany) 的项目中获取 'diversities()'。
我错过了什么?:
** Client 工作正常并且 Item 和 Client 完全相同(几乎)。 我得到的错误是
未找到基础 table 或视图:1146 Table 'restigo_spm.diversity_item' 不存在
而且我在代码中的任何地方都没有 diversity_item,为什么要搜索它?
客户:
class Client extends Model
{
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(枢轴):
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');
}
}
项目架构:
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 不行。 项目控制器:
class ItemController extends Controller
{
public static function index()
{
$items = Item::with('diversities')->get();
return new ItemCollection($items);
}
因为下面的方法。
public function diversities()
{
return $this->belongsToMany('App\Models\Diversity');
}
您正在使用 belongsToMany
,这是多对多关系。由于您没有明确定义中间 table 的 table 名称,它根据命名约定创建它。它假定您的中间 table 是第一个 diversity
+ _
和 item
.
您可以使用 client_diversity
作为 diversities
方法的第二个参数。
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: