0
votes

I am trying to insert single data into db in Laravel 4 but failed to do so after trying for 3 hrs.

Below is code snippet,

Model

User.php

class User extends Eloquent
{
  protected $table='users';
  protected $protected = array();
}

Migration

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

}

Controller

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'));

But I am not able to even insert into database.

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,
    ),

),

);

Screenshot

enter image description here

Latest One after setting debug => true in app.php

enter image description here

2
Have you selected the correct database? Does your database have the table? Are you sure Input::get('name') and Input::get('cntno') are set? Just some suggestions :) - Jerodev
Input::get('name') and Input::get('cntno') both are having valid values(i.e. they are set), How to know that you have selected the correct database or table? I have changed the database.php pls check the edit. - krohit
Have you runned your migrations? - Jerodev
What error do you get? I can see a few things that might cause it - David Jones
You need to turn debug to true in your app/config/app.php file to get more information - David Jones

2 Answers

2
votes

See the Laravel docs for Mass Assignment. All you need to do add a fillable property to your model:

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

That will allow you to 'mass assign' your properties in a create method.


public function post_create()
{
    $user = new User;

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

    $user->save();
}
1
votes

Refer: MassAssignmentException in Laravel

Laravel provide by default a protection against mass assignment security issues. That's why you have to manually define which fields could be "mass assigned"

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