【问题标题】:Simple insert using eloquent in Laravel 4在 Laravel 4 中使用 eloquent 进行简单插入
【发布时间】: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 以获取更多信息

标签: php mysql laravel-4


【解决方案1】:

请参阅Laravel docs for Mass Assignment。您只需将fillable 属性添加到您的模型:

protected $fillable = array('name', 'cntNo');

这将允许您在 create 方法中“批量分配”您的属性。


或者,您可以满足一个新的用户对象并单独设置它的值:
public function post_create()
{
    $user = new User;

    $user->name = Input::get('name');
    $user->cntNo = Input::get('cntno');

    $user->save();
}

【讨论】:

  • 谢谢!!那行得通..!顺便说一句,我使用了 protected $protected = array();,它是 $fillable 的对应物,为什么它仍然不起作用?
  • 其实你想的属性是guardedguarded 也包含在 mass assignment docs
【解决方案2】:

参考: MassAssignmentException in Laravel

Laravel 默认提供针对批量分配安全问题的保护。这就是为什么您必须手动定义哪些字段可以“批量分配”

class User extends Eloquent
{
  protected $table='users';
  protected $guarded = array('id');
  protected $fillable = array('name', 'cntNo');
}

【讨论】:

  • 嗨 abhijith,我认为是 $guarded 而不是 $protected
猜你喜欢
  • 2012-09-24
  • 1970-01-01
  • 2015-01-07
  • 2014-07-07
  • 1970-01-01
  • 2014-01-01
  • 2023-03-08
  • 1970-01-01
  • 2019-08-01
相关资源
最近更新 更多