У меня есть много разных отношений между продуктами и таблицами product_categories. Теперь я хочу создать таблицу переводов для категорий.Laravel: Eloquent Ошибка связи
Но как я отношусь к ним красноречиво? Я изучаю отношения с базой данных, так что простите мое невежество.
Вот мои таблицы: Модель
//languages
Schema::create('locales', function(Blueprint $table)
{
$table->increments('id');
$table->string('code', 2);
$table->string('name');
});
//products
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
$table->string('thumbnail');
$table->timestamps();
});
//categories
Schema::create('product_categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('category');
});
//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('product_category_id')->unsigned()->index(); // the id of the picnic that this bear is at
$table->foreign('product_category_id')->references('id')->on('product_categories');
});
//category translations
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 extends Model
{
protected $table = 'products';
public function product_category() {
return $this->belongsToMany('App\product_category', 'product_productcategory')->translation(1);
}
}
Категории продуктов:
class product_category extends Model
{
protected $table = 'product_categories';
public function product() {
return $this->belongsToMany('App\Product', 'product_productcategory');
}
}
Категория Переводы
class Category_Translation extends Model
{
protected $table = 'category_translations';
public function product_category() {
return $this->belongsTo('App\product_category');
}
}
Но когда я бегу:
$product = App\Product::find(1)->first();
echo $product->product_category;
Я получаю сообщение об ошибке:
Call to undefined method Illuminate\Database\Query\Builder::translation()
Большое спасибо за ответ. Я изменил свой код на ваш, и у меня есть следующая ошибка: Звонок на неопределенный метод Illuminate \ Database \ Eloquent \ Collection :: translIn() – romin
Можете ли вы вставить свой фактический код в какой-нибудь пастебин? –
Я загрузил свой проект для проверки: Запуск migrate и db: seed ... он создаст для вас все таблицы и содержимое базы данных ... игнорируйте коды, связанные с статьями, которые я только что добавил. https://www.dropbox.com/s/x14y2tf3xos0rbo/backend.zip?dl=0 – romin