我正在尝试为学校制作以下教程:http://www.gdm.gent/nmdad2/laravel/(荷兰语)
但是当谈到跑步时:
phpunit tests/SeederTest.php
我收到以下错误:
1) SeederTest::testUsersTableSeeder Illuminate\Database\QueryException:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'underthehammer_local.users' doesn't exist (SQL: select count(*) as aggregate
from `users` where (`email` = underthehammer_gebruiker@arteveldehs.be and
`name` = underthehammer_gebruiker and `given_name` = NMDAD-II and
`family_name` = Gebruiker))
以下是我的其他文件:
QueryException:
<?php
namespace Illuminate\Database;
use PDOException;
class QueryException extends PDOException
{
/**
* The SQL for the query.
*
* @var string
*/
protected $sql;
/**
* The bindings for the query.
*
* @var array
*/
protected $bindings;
/**
* Create a new query exception instance.
*
* @param string $sql
* @param array $bindings
* @param \Exception $previous
* @return void
*/
public function __construct($sql, array $bindings, $previous)
{
parent::__construct('', 0, $previous);
$this->sql = $sql;
$this->bindings = $bindings;
$this->previous = $previous;
$this->code = $previous->getCode();
$this->message = $this->formatMessage($sql, $bindings, $previous);
if ($previous instanceof PDOException) {
$this->errorInfo = $previous->errorInfo;
}
}
/**
* Format the SQL error message.
*
* @param string $sql
* @param array $bindings
* @param \Exception $previous
* @return string
*/
protected function formatMessage($sql, $bindings, $previous)
{
return $previous->getMessage().' (SQL: '.str_replace_array('\?', $bindings, $sql).')';
}
/**
* Get the SQL for the query.
*
* @return string
*/
public function getSql()
{
return $this->sql;
}
/**
* Get the bindings for the query.
*
* @return array
*/
public function getBindings()
{
return $this->bindings;
}
}
SeederTest:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SeederTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testUsersTableSeeder()
{
$this->seeInDatabase(CreateUsersTable::TABLE, [
'email' => 'underthehammer_gebruiker@arteveldehs.be',
'name' => 'underthehammer_gebruiker',
'given_name' => 'NMDAD-II',
'family_name' => 'Gebruiker',
]);
}
}
CreateUserTable:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
const MODEL = 'user';
const TABLE = self::MODEL.'s';
const PRIMARY_KEY = 'id';
const FOREIGN_KEY = self::MODEL.'_'.self::PRIMARY_KEY;
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(self::TABLE, function (Blueprint $table) {
// Primary Key
$table->increments(self::PRIMARY_KEY);
// Data
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->string('given_name');
$table->string('family_name');
// Meta Data
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop(self::TABLE);
}
}
UserTableSeeder:
<?php
use App\User;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'email' => 'underthehammer_gebruiker@arteveldehs.be',
'name' => 'underthehammer_gebruiker',
'password' => Hash::make('underthehammer_wachtwoord'),
'given_name' => 'NMDAD-II',
'family_name' => 'Gebruiker',
]);
// Faker
// -----
factory(User::class, DatabaseSeeder::AMOUNT['DEFAULT'])->create();
}
}
DatabaseSeeder:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
const AMOUNT = [
'NONE' => 0,
'MIN' => 1,
'FEW' => 3,
'SOME' => 5,
'DEFAULT' => 10,
'MANY' => 100,
'MAX' => 1000,
];
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Seeder order is important for database relations!
$seeders = [
UsersTableSeeder::class,
CategoriesTableSeeder::class,
TagsTableSeeder::class,
PostsTableSeeder::class,
];
$i = 0;
foreach ($seeders as $seeder) {
$count = sprintf('%02d', ++$i);
$this->command->getOutput()->writeln("<comment>Seed${count}:</comment> ${seeder}...");
$this->call($seeder);
}
}
}
数据库配置:
<?php
return [
/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/
'fetch' => PDO::FETCH_CLASS,
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];
希望有人可以帮助我。
答案 0 :(得分:0)
这是因为您在迁移中有另一个具有相同名称的表。 比如login.php有一个表&#34;用户&#34; register.php有一个表格&#34; users&#34; 给出不同的名字 检查并重新运行&#34; php artisan migration&#34; 它会起作用。