php - 在 Illuminate\Database\QueryException 处找不到列错误?

标签 php laravel laravel-artisan laravel-7 laravel-seeding

我在 Laravel 应用程序中工作,并通过 user_id 列将 users 表与 posts 表相关联。 posts 表中已有 user_id 列。但是当我运行 php artisan db:seed 命令时,出现错误:

`SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id ' in 'field list'

出现以下错误:

   Illuminate\Database\QueryException 

  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id ' in 'field list' (SQL: insert into `posts` (`user_id `, `title`, `post_image`, `content`, `user_id`, `updated_at`, `created_at`) values (11, Perferendis dolorem eum ex nesciunt., https://lorempixel.com/900/300/?54025, Unde voluptatibus praesentium unde et illum qui quo. Cupiditate pariatur aperiam fugiat perspiciatis. In atque corrupti eius ipsam saepe blanditiis minima., 1, 2020-06-28 08:54:02, 2020-06-28 08:54:02))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675| 

  • A column was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`. 
    https://laravel.com/docs/master/migrations#running-migrations

      +13 vendor frames 
  14  database/seeds/DatabaseSeeder.php:21
      Illuminate\Database\Eloquent\Relations\HasOneOrMany::save()

      +1 vendor frames 
  16  database/seeds/DatabaseSeeder.php:22
      Illuminate\Support\Collection::each()

最佳答案

我认为在用户模型的关系函数中,user_id 之后有一个空格。或者你需要找到user_id之后你在哪里使用空格。 Check there is space after user_id where do you want store

关于php - 在 Illuminate\Database\QueryException 处找不到列错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62620900/

相关文章:

php - 基于 URL 长度的 API 性能

php - Laravel 7.x非法偏移类型

php - Laravel "artisan make:observer"失败

php - 服务器,PHP 文件丢失, fatal error

php - 将可变数量的参数传递到 PDO 语句并返回 JSON 对象

php - 我的 PHP 电话簿 CRUD 项目出现错误

javascript - 重复显示数据

php - 除非路由被命名,否则 Laravel 无法找到路由

php - 如何在 Laravel 中通知用户作业已完成?

php - 无法打开流 : No such file or directory on Homestead environment