Laravel-在bool上调用成员函数categorie()

问题描述 投票:0回答:1

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

[当我尝试使用db:seed时,我得到了:

ProductSeeder上的错误:在bool上调用成员函数category()

这是我的代码:

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

DatabaseSeeder

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

CategorieSeeder

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

感谢您的帮助。

php laravel eloquent categories crud
1个回答
0
投票

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

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',
    ];
}
© www.soinside.com 2019 - 2024. All rights reserved.