Laravel - 调用 bool 上的成员函数 categorie()
Laravel - Call to a member function categorie() on bool
我正在尝试将我的类别附加到我的产品。
当我尝试 db:seed 我得到:
ProductSeeder 错误:调用 bool 上的成员函数 categories()
这是我的代码:
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();
});
}
Category.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);
}
}
数据库播种器
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(CategorieSeeder::class);
$this->call(ProductSeeder::class);
}
分类播种器
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);
}
}
}
感谢您的帮助。
不要使用 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',
];
}
我正在尝试将我的类别附加到我的产品。
当我尝试 db:seed 我得到:
ProductSeeder 错误:调用 bool 上的成员函数 categories()
这是我的代码:
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();
});
}
Category.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);
}
}
数据库播种器
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(CategorieSeeder::class);
$this->call(ProductSeeder::class);
}
分类播种器
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);
}
}
}
感谢您的帮助。
不要使用 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',
];
}