我希望这个帖子能让每个人都健康.
以下代码有什么问题?我的多对多关系出错了.在过去的4个小时里,我遇到了这个问题.如果您发现问题的bug或来源,我将非常感激.
这是我的代码和表格:
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
});
Run Code Online (Sandbox Code Playgroud)
产品型号:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'App\product_productcategory', 'product_id', 'productcategories_id');
}
}
Run Code Online (Sandbox Code Playgroud)
和枢轴模型
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'App\product_productcategory', 'productcategories_id', 'product_id');
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
更新:由于@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']);
});
Run Code Online (Sandbox Code Playgroud)
我的产品类别与翻译有新的关系
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在类别翻译中:
class Category_Translation extends Model
{
protected $table = 'category_translations';
public function product_category() {
return $this->belongsTo('App\product_category');
}
}
Run Code Online (Sandbox Code Playgroud)
最后我的产品:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我跑的时候:
$product = App\Product::find(1)->first();
echo $product->product_category;
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Call to undefined method Illuminate\Database\Query\Builder::translation()
Run Code Online (Sandbox Code Playgroud)
您的迁移文件错误:
//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');
});
Run Code Online (Sandbox Code Playgroud)
此代码将在数据库级别上建立关系.
当你有12分钟的空闲时间时,你可以查看本教程,了解laravel中的多对多关系:https://laracasts.com/series/laravel-5-fundamentals/episodes/21
更新:
你还需要更新你的关系:
class Product extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'product_productcategory');
}
}
Run Code Online (Sandbox Code Playgroud)
和:
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'product_productcategory');
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2252 次 |
| 最近记录: |