简体   繁体   English

尝试在 Laravel 中播种表时出错

[英]Getting errors when trying to seed tables in Laravel

When I'm trying to execute php artisan migrate:fresh --seed command I'm getting errors.当我尝试执行php artisan migrate:fresh --seed命令时出现错误。 I don't understand what they are all about and I don't know what could cause them.我不明白它们是怎么回事,我不知道是什么导致了它们。 The description of the problem is below.问题描述如下。

Factory file PostFactory.php:工厂文件 PostFactory.php:

<?php

namespace Database\Factories;

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

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $title = $faker->realText(rand(10, 40));
        $short_title = \Illuminate\Support\Str::length($title) > 30 ? mb_substr($title, 0, 30) . "..." : $title;
        $created = $faker->dateTimeBetween("-30days", "-1days");

        return [
            "title" => $title,
            "short_title" => $short_title,
            "author_id" => rand(1, 4),
            "descr" => $faker->realText(100, 500),
            "created_at" => $created,
            "updated_at" => $created
        ];
    }
}

DatabaseSeeder.php: 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()->count(4)->create();
        \App\Models\Post::factory()->count(15)->create();
    }
}

When I execute php artisan migrate:fresh --seed I'm getting those errors:当我执行 php artisan migrate:fresh --seed 我收到这些错误:

Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002]   , ..      .
 (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

  at C:\xampp\htdocs\laralove\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a   
    690▕         // lot more helpful to the developer instead of just the database's errors.     
   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002]   , ..      .
 (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

  at C:\xampp\htdocs\laralove\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕

  1   C:\xampp\htdocs\laralove\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDOException::("SQLSTATE[HY000] [2002]   , ..      .
")

  2   C:\xampp\htdocs\laralove\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDO::__construct("mysql:host=localhost;port=3306;dbname=laralove", "root", "", [])

I've checked the lines in the files provided in the error description but I've no clue what can be causing the errors.我检查了错误描述中提供的文件中的行,但我不知道是什么导致了错误。

Any help would be very appriciated任何帮助将非常appriciated


UPD 0:更新 0:

I've tried to change 'localhost' to '127.0.0.1' in config files as @Rwd suggested.我尝试按照@Rwd 的建议将配置文件中的“localhost”更改为“127.0.0.1”。 And previous errors are gone.之前的错误都消失了。 But now I'm getting other errors:但现在我遇到了其他错误:

ErrorException 

  Undefined variable: faker

  at C:\xampp\htdocs\laralove\database\factories\PostFactory.php:24
     20▕      * @return array
     21▕      */
     22▕     public function definition()
     23▕     {
  ➜  24▕         $title = $faker->realText(rand(10, 40));
     25▕         $short_title = \Illuminate\Support\Str::length($title) > 30 ? mb_substr($title, 0, 30) . "..." : $title;
     26▕         $created = $faker->dateTimeBetween("-30days", "-1days");
     27▕
     28▕         return [

  1   C:\xampp\htdocs\laralove\database\factories\PostFactory.php:24
      Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: faker", "C:\xampp\htdocs\laralove\database\factories\PostFactory.php", [])

  2   C:\xampp\htdocs\laralove\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:385
      Database\Factories\PostFactory::definition()

UPD 1:更新 1:

Replaced $faker with $this->faker as @Cristea suggested and now getting those previous errors:正如@Cristea 建议的那样,将$faker替换$faker $this->faker ,现在得到了以前的错误:

  1   C:\xampp\htdocs\laralove\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDOException::("SQLSTATE[HY000] [2002]   , ..      .
")

  2   C:\xampp\htdocs\laralove\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDO::__construct("mysql:host=localhost;port=3306;dbname=laralove", "root", "", [])

localhost is still changed to 127.0.0.1 in .env and database.php as @Rwd suggested正如@Rwd 建议的那样,localhost 在 .env 和 database.php 中仍然更改为 127.0.0.1

只需将$faker替换$faker $this->faker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 进入 Container.php 第 752 行:尝试在 Laravel 中播种角色时,类 RoleTableSeeder 不存在错误 - Getting In Container.php line 752: Class RoleTableSeeder does not exist error, when trying to seed a role in Laravel Laravel 使用 factory-&gt;create() 修补程序时出错,但在 artisan 中运行迁移/种子命令时不会出错 - Laravel Errors in tinker using factory->create() but not when running migration/seed commands in artisan laravel上的Json数据种子时尝试获取非对象的属性 - Trying to get property of non-object when Json data seed on laravel Laravel(5.6)如何为具有其他表关系的表播种 - Laravel (5.6) how to seed tables with relationships to other tables 尝试联接表时出现MySQL子查询错误(建立评级系统) - Mysql subquery errors when trying to join tables (making rating system) Laravel 5.6-使用php artisan db:seed时出现错误 - Laravel 5.6 - Getting an error when I use php artisan db:seed Laravel没有得到404错误 - Laravel Not getting 404 errors Laravel 6 Seed DB 当关系在 1 个表上时 - Laravel 6 Seed DB When relation is on 1 table 我正在尝试安装 Laravel 微风,但我不断收到 Laravel Mix 错误 - I'm trying to install Laravel breeze but I keep getting Laravel Mix errors 用Laravel 5.4为phpunit我测试的几张表播种 - Seed once several tables for phpunit I testing with Laravel 5.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM