laravel雄辩不定表

fae0ux8s  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(440)

我已经创建了一个数据库种子播种器来为我的表播种,这是我几秒钟前通过迁移创建的。但是,在运行播种机时出现以下错误:

In Connection.php line 664:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'trier_taxibus.ca
  tegories' doesn't exist (SQL: insert into `categories` (`category`, `update
  d_at`, `created_at`) values (ISF, 2018-07-10 14:16:08, 2018-07-10 14:16:08)
  )

In Connection.php line 452:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'trier_taxibus.ca
  tegories' doesn't exist

sql语句在我看来很好,如果我复制并粘贴到phpmyadmin,它也能正常工作。我还创建了一个模型并定义了表名以及可填充的列..:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'categories';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'category'
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}

这是我的数据库播种器:

use Illuminate\Database\Seeder;
use App\Category;

class CategoryTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $category = new Category();
        $category->category = 'ISF';
        $category->save();
    }
}

如果我选择了表,这就是返回的结果 categories 在我的工具sequel pro中:

CREATE TABLE `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

我觉得一切都很好。然而,我在上面得到了这个错误。有什么想法吗?
谨致问候

ktca8awb

ktca8awb1#

您应该在.env文件中检查db连接,或者可以在模型中尝试此操作:

protected $connection = 'trier_taxibus';
bvk5enib

bvk5enib2#

你可以使用下面的代码

public function run()
    {
        $row = array('category' => 'Purchasing',
                    'created_at'    => Carbon::now(),
                    'updated_at'    => Carbon::now());
        DB::table('categories')->insert($row);
    }

口才

t1rydlwq

t1rydlwq3#

似乎是因为在模型中将$table保持为“protected”,所以出现了错误。将其更改为“public”。
这样地:

public $table = "categories";
protected $primaryKey = "id";

如果添加了正确的数据库、用户名和密码,请检查.env文件。

相关问题