Я пытаюсь прикрепить свои категории к моим продуктам.
Когда я пытаюсь получить db: seed, я получаю:
Ошибка на ProductSeeder: вызов категории функций-членов ( ) на BOOL
1006 * Вот мой код: 1008 * 2020_04_09_073846_create_products_table
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('categoryId')->unsigned();
$table->string('name');
$table->string('slug');
$table->string('category');
$table->string('description');
$table->string('releaseDate');
$table->float('price');
$table->timestamps();
});
}
2020_05_02_201337_create_categories_table
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('slug')->unique();
$table->timestamps();
});
}
2020_05_03_105839_create_category_product_table
public function up()
{
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->nullable();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
Категория. php
class Category extends Model
{
public function products()
{
return $this->belongsToMany('App\Product');
}
}
Product. php
class Product extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category');
}
public function presentPrice()
{
return money_format('$%i', $this->price / 100);
}
}
DatabaseSeeder
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(CategorieSeeder::class);
$this->call(ProductSeeder::class);
}
CategorySeeder
use Carbon\Carbon;
use App\Category;
use Illuminate\Database\Seeder;
class CategorieSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$now = Carbon::now()->toDateTimeString();
DB::table('categories')->insert(
array(
array(
'name' => 'Xbox',
'slug' => 'xbox',
),
array(
'name' => 'Playstation',
'slug' => 'playstation',
),
ProductSeeder
use Illuminate\Database\Seeder;
use App\Product;
class ProductSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
for ($i=0; $i < 30; $i++) {
DB::table('products')->insert([
'name' => 'Halo 5',
'slug' => 'halo-5',
'categoryId' => '1',
'category' => 'Xbox One',
'description' => "Halo 5: Guardians sur Xbox One est un FPS mettant en scène les
aventures du Master Chief et d'un nouveau personnage, le Spartan Jameson Locke. ",
'releaseDate' => '27 octobre 2015',
'price' => '54.99',
])->categories()->attach(1);
}
}
}
Спасибо за помощь.