【问题标题】:Larvel 8 :- Array to string conversionLaravel 8 :- 数组到字符串的转换
【发布时间】:2021-07-16 14:20:00
【问题描述】:

运行命令时出现标题错误php artisan db:seed。我想将数据存储在数据库中。迁移已经完成。但是在播种时会显示错误。我不知道这个问题是从哪里来的。

ProductFactory.php

<?php

namespace Database\Factories;

use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;

class ProductFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Product::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->word,
            'detail' => $this->faker->paragraphs,
            'price' => $this->faker->numberBetween(100, 1000),
            'stock' => $this->faker->randomDigit(),
            'discount' => $this->faker->numberBetween(2, 30),
        ];
    }
}

DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();
        \App\Models\Product::factory(50)->create();
       
    }
}

【问题讨论】:

  • 确切的错误是什么?它应该为您指明正确的方向。我第一眼看不出有什么问题
  • @Flame 我编辑问题请检查屏幕截图
  • dd($var) 来自definition() 函数的数组,它似乎包含一个数组?或者它发生在将updated_at/created_at 添加到对象的过程中。

标签: laravel laravel-seeding


【解决方案1】:

$this-&gt;faker-&gt;paragraphs()$this-&gt;faker-&gt;words 可选地接受两个参数,段落和单词的数量,以及是否返回为数组或字符串的布尔值

使用这个

'detail' => $this->faker->paragraph,

代替:

'detail' => $this->faker->paragraphs,

【讨论】:

    【解决方案2】:

    使用

    'detail' => $this->faker->paragraph,
    

    而不是

    'detail' => $this->faker->paragraphs,
    

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2017-01-14
      • 2020-09-10
      相关资源
      最近更新 更多