首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel -对bool上的成员函数categorie()的调用

Laravel -对bool上的成员函数categorie()的调用
EN

Stack Overflow用户
提问于 2020-05-03 21:23:29
回答 1查看 1.2K关注 0票数 0

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

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

ProductSeeder出错:调用布尔值上的成员函数categories()

下面是我的代码:

2020_04_09_073846_create_products_table

代码语言:javascript
复制
    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

代码语言:javascript
复制
    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

代码语言:javascript
复制
    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

代码语言:javascript
复制
class Category extends Model
{
    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
}

Product.php

代码语言:javascript
复制
class Product extends Model
{
    public function categories()
    {
        return $this->belongsToMany('App\Category');
    }
    public function presentPrice()
    {
        return money_format('$%i', $this->price / 100);
    }
}

DatabaseSeeder

代码语言:javascript
复制
    public function run()
    {
        // $this->call(UsersTableSeeder::class);
        $this->call(CategorieSeeder::class);
        $this->call(ProductSeeder::class);
    }

CategorieSeeder

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
        }
    }
}

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-03 21:42:56

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

代码语言:javascript
复制
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);

在使用模型时,您必须设置文件,这是避免设置意外属性的保护措施。

代码语言:javascript
复制
class Product extends Model {
    protected $fillable = [
        'name',
        'slug',
        'categoryId',
        'category',
        'description',
        'releaseDate',
        'price',
    ];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61575150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档