【发布时间】:2014-11-30 15:19:20
【问题描述】:
我正在尝试将单个数据插入 Laravel 4 中的数据库,但尝试 3 小时后未能这样做。
下面是代码sn-p,
型号
用户.php
class User extends Eloquent
{
protected $table='users';
protected $protected = array();
}
迁移
class CreateUserInfoTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('name');
$table->string('cntNo');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
控制器
UserController.php
public function post_create()
{
User::create(array('name'=>Input::get('name'),'cntNo'=>Input::get('cntno')));
}
Routes.php
Route::post('new/create', array('uses'=>'UserController@post_create'));
但我什至无法插入数据库。
app-->config-->database.php
<?php
return array(
/*
|--------------------------------------------------------------------------
| 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' => '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' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel_user',
'username' => 'root',
'password' => 'sa',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
/*
|--------------------------------------------------------------------------
| 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' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
截图
在 app.php
中设置 debug => true 后的最新版本【问题讨论】:
-
您选择了正确的数据库吗?你的数据库有表吗?您确定
Input::get('name')和Input::get('cntno')已设置吗?只是一些建议:) -
Input::get('name')和Input::get('cntno')都具有有效值(即它们已设置),如何知道您选择了正确的数据库或表?我已经更改了database.php请检查编辑。 -
您是否已运行迁移?
-
你得到什么错误?我可以看到一些可能导致它的事情
-
您需要在您的 app/config/app.php 文件中将 debug 设置为 true 以获取更多信息