Laravel-调用布尔上的成员函数category()



我正在尝试将我的类别附加到我的产品中。

当我尝试db:seed时,我得到:

ProductSeeder错误:调用布尔上的成员函数categories((

这是我的代码:

2020_04_09_073846_创建_产品_表格

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();
});
}

Category.php

class Category extends Model
{
public function products()
{
return $this->belongsToMany('AppProduct');
}
}

产品.php

class Product extends Model
{
public function categories()
{
return $this->belongsToMany('AppCategory');
}
public function presentPrice()
{
return money_format('$%i', $this->price / 100);
}
}

数据库种子程序

public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(CategorieSeeder::class);
$this->call(ProductSeeder::class);
}

类别种子

use CarbonCarbon;
use AppCategory;
use IlluminateDatabaseSeeder;
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 IlluminateDatabaseSeeder;
use AppProduct;
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);
}
}
}

谢谢你的帮助。

不要使用Db::table,而是使用模型,这里是您已经创建的产品模型。

Product::create([
'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);

当使用模型时,您必须设置可填充项,这是一种保护措施,以避免设置意外的属性。

class Product extends Model {
protected $fillable = [
'name',
'slug',
'categoryId',
'category',
'description',
'releaseDate',
'price',
];
}

最新更新