Kar*_*ski 0 php laravel laravel-4 laravel-routing
我的routes.php我有以下内容:
<?php
Route::group(array(), function () {
View::share('roots', Category::roots()->get());
$tree = Category::get()->toHierarchy()->toArray();
View::share('categories', $tree);
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'));
});
Run Code Online (Sandbox Code Playgroud)
当我的数据库还没有表,我想做php artisan migrate时,结果是:SQLSTATE [42S02]:找不到基表或视图:1146表'ae_dev.categories'不存在
我的迁移文件:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('categories', function(Blueprint $table) {
$table->increments('id');
$table->integer('parent_id')->nullable()->index();
$table->integer('lft')->nullable()->index();
$table->integer('rgt')->nullable()->index();
$table->integer('depth')->nullable();
$table->string('name', 255);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::drop('categories');
}
}
Run Code Online (Sandbox Code Playgroud)
我认为Laravel托盘从routes.php调用Category并想做select或somethink所以我想运行创建类别表的迁移,但上面的错误是在...之前生成的
我怎样才能解决这个问题?
似乎所有php artisan命令都使用routes.php文件,因此当您尝试访问此文件以访问数据库表(并且由于您尚未运行迁移而不存在表)时,您将收到此错误.你不能跑,php artisan migrate因为你得到这个错误.
一种解决方案是删除查询数据库的代码,但这当然不是一个好的解决方案.所以你应该做的是从你做过的第一次迁移中选择表(在你的情况下它可能是categories.稍后你有更多的迁移,但这将是第一次)并添加类似的东西:
if (!Schema::hasTable('categories'))
{
return;
}
Run Code Online (Sandbox Code Playgroud)
进入你的routes.php文件.
但是,如果您将使用迁移进行更多操作并且还需要其他表查询,则需要将上述条件更改为:
if (!Schema::hasTable('categories') || !Schema::hasTable('users'))
{
return;
}
Run Code Online (Sandbox Code Playgroud)
但它仍然会导致一些问题 - 你不想每次都在你的路线中运行这段代码,所以我会这样做:
if ($env == 'local') {
if (!Schema::hasTable('categories') || !Schema::hasTable('users'))
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
您当然需要配置您的环境.但是现在您只为本地环境运行此代码,在生产时,此代码将不会运行,因此不会影响应用程序性能.当然,我在这里假设你不会在制作上与工匠一起玩.
编辑
但是如果你只使用查询来共享数据(而不是路由),我会移动这些行:
View::share('roots', Category::roots()->get());
$tree = Category::get()->toHierarchy()->toArray();
View::share('categories', $tree);
Run Code Online (Sandbox Code Playgroud)
以BaseController和在扩展它的所有控制器的运行方法(或父构造).
旧答案(在这种情况下不够)
错误信息足够清楚,您的解释是您的数据库中还没有表.如果没有表,则无法运行数据库查询.
在迁移中,您应该创建必要的表,例如,这是用户表的迁移:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class NewUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function ($table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('user_name', 60)->unique();
$table->string('email', 120)->unique();
$table->string('passwd', 256);
$table->decimal('balance', 8, 2);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('users');
}
}
Run Code Online (Sandbox Code Playgroud)
使用迁移时也不应该将数据插入数据库(正如您现在所做的那样).你应该在播种表时这样做.