Laravel雄辩无法确定表

时间:2018-07-10 12:35:45

标签: php mysql laravel eloquent laravel-seeding

我创建了一个数据库播种器来播种我的表,该表是我几秒钟前通过迁移创建的。但是,运行播种机时出现此错误:

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();
    }
}

如果我在工具Sequel Pro中选择表categories,就会得到以下结果:

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;

对我来说似乎一切都很好。但是,我在上面收到此错误。有什么想法吗?

亲切的问候

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码

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

口才

的人

答案 1 :(得分:1)

您应该在.env文件中检查数据库连接,或者您可以在模型中尝试使用它:

protected $connection = 'trier_taxibus'; 

答案 2 :(得分:0)

由于您将$ table保留为模型中的“受保护”,因此似乎出现了错误。将其更改为“公开”。

赞:

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

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