Laravel BelongsToMany错误-找不到基表或视图
我希望这条线能让每个人都健康 以下代码有什么问题?我的多对多关系出现了错误。在过去的4个小时里,我一直被这个问题困扰着。如果您能找到bug或问题的根源,我将非常感激 以下是我的代码和表格:Laravel BelongsToMany错误-找不到基表或视图,laravel,eloquent,Laravel,Eloquent,我希望这条线能让每个人都健康 以下代码有什么问题?我的多对多关系出现了错误。在过去的4个小时里,我一直被这个问题困扰着。如果您能找到bug或问题的根源,我将非常感激 以下是我的代码和表格: Schema::create('products', function(Blueprint $table) { $table->increments('id'); $table->integer('category'); $table->
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->integer('category');
$table->string('thumbnail');
$table->string('images');
$table->string('logo');
$table->string('main_photo');
$table->timestamps();
});
//categories table
Schema::create('product_categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('category');
});
//relationship table
Schema::create('product_productcategory', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_id'); // the id of the bear
$table->integer('productcategories_id'); // the id of the picnic that this bear is at
});
产品型号:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'App\product_productcategory', 'product_id', 'productcategories_id');
}
}
和枢轴模型
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'App\product_productcategory', 'productcategories_id', 'product_id');
}
}
提前谢谢
更新:由于@mimo,问题得以解决。 但是现在我想用不同的语言翻译类别,所以我创建了一个如下表:
Schema::create('category_translations', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('cascade');
$table->integer('locale_id')->unsigned();
$table->foreign('locale_id')->references('id')->on('locales')->onDelete('cascade');
$table->unique(['category_id', 'locale_id']);
});
以及我的产品类别与翻译的新关系
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
//return $this->belongsToMany('App\Product', 'App\product_productcategory', 'productcategories_id', 'product_id');
return $this->belongsToMany('App\Product', 'product_productcategory');
}
public function translation($lang) {
return $this->hasMany('App\Category_Translation')->where('locale_id', '=', $lang);
}
}
类别翻译:
class Category_Translation extends Model
{
protected $table = 'category_translations';
public function product_category() {
return $this->belongsTo('App\product_category');
}
}
最后是我的产品:
class Product extends Model
{
protected $table = 'products';
public function translation($lang) {
return $this->hasMany('App\Product_Translation')->where('locale_id', '=', $lang);
}
public function product_category() {
//return $this->belongsToMany('App\product_category', 'App\product_productcategory', 'product_id', 'productcategories_id');
//return $this->belongsToMany('App\product_category', 'product_productcategory');
return $this->belongsToMany('App\product_category', 'product_productcategory')->translation(1);
}
}
现在当我跑步时:
$product = App\Product::find(1)->first();
echo $product->product_category;
我得到一个错误:
Call to undefined method Illuminate\Database\Query\Builder::translation()
您的迁移文件错误:
//relationship table
Schema::create('product_productcategory', function(Blueprint $table)
{
$table->integer('product_id')->unsigned()->index(); // the id of the bear
$table->foreign('product_id')->references('id')->on('products');
$table->integer('productcategories_id')->unsigned()->index(); // the id of the picnic that this bear is at
$table->foreign('productcategories_id')->references('id')->on('product_categories');
});
此代码将在数据库级别创建关系
当您有12分钟的空闲时间时,您可以查看关于laravel中多对多关系的本教程:
更新:
您还需要更新您的关系:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'product_productcategory');
}
}
以及:
您的迁移文件错误:
//relationship table
Schema::create('product_productcategory', function(Blueprint $table)
{
$table->integer('product_id')->unsigned()->index(); // the id of the bear
$table->foreign('product_id')->references('id')->on('products');
$table->integer('productcategories_id')->unsigned()->index(); // the id of the picnic that this bear is at
$table->foreign('productcategories_id')->references('id')->on('product_categories');
});
此代码将在数据库级别创建关系
当您有12分钟的空闲时间时,您可以查看关于laravel中多对多关系的本教程:
更新:
您还需要更新您的关系:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'product_productcategory');
}
}
以及:
非常感谢您的回复,我用您的代码更改了代码,但仍然是相同的错误。。。My route.php代码:$product=App\product::find(1)->product_category()->first();echo$产品;错误消息:SQLSTATE[42S02]:找不到基表或视图:1146表“project.app\product\u productcategory”不存在错误,您也必须更新模型。更新了我的答案非常感谢!!!。它解决了问题…我仍然不知道问题出在哪里,因为我已经手动定义了列名?还有一个问题。请原谅我的无知,我对db关系没有经验。如何组合这两个对象,即产品和产品类别。因为现在我必须执行两个单独的请求,比如:App\Product::find(1)->first();获取产品本身和另一个请求App\product::find(1)->product_category()->first();获取产品类别。您可以使用
App\product->width('product\u category')->查找(1)代码>。它将返回一个已加载类别的产品。我不知道如何感谢您。你今天救了我一命。但现在还有最后一个问题。。。我现在已经创建了一个翻译表,但我不知道如何在laravel中将它们关联起来。请检查我更新的问题,我已经发布了我所做的代码。非常感谢您的回复,我用您的代码更改了代码,但仍然是相同的错误。。。My route.php代码:$product=App\product::find(1)->product_category()->first();echo$产品;错误消息:SQLSTATE[42S02]:找不到基表或视图:1146表“project.app\product\u productcategory”不存在错误,您也必须更新模型。更新了我的答案非常感谢!!!。它解决了问题…我仍然不知道问题出在哪里,因为我已经手动定义了列名?还有一个问题。请原谅我的无知,我对db关系没有经验。如何组合这两个对象,即产品和产品类别。因为现在我必须执行两个单独的请求,比如:App\Product::find(1)->first();获取产品本身和另一个请求App\product::find(1)->product_category()->first();获取产品类别。您可以使用App\product->width('product\u category')->查找(1)代码>。它将返回一个已加载类别的产品。我不知道如何感谢您。你今天救了我一命。但现在还有最后一个问题。。。我现在已经创建了一个翻译表,但我不知道如何在laravel中将它们关联起来。请检查我更新的问题,我已经发布了我所做的代码。非常感谢。