0

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

12
  • 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 :) Commented Oct 6, 2014 at 11:14
  • 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. Commented Oct 6, 2014 at 11:20
  • 1
    Have you runned your migrations? Commented Oct 6, 2014 at 11:27
  • 1
    What error do you get? I can see a few things that might cause it Commented Oct 6, 2014 at 11:28
  • 2
    You need to turn debug to true in your app/config/app.php file to get more information Commented Oct 6, 2014 at 11:54

2 Answers 2

2

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.


Alternatively, you could insatiate a new User object and individually set it's values:

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

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

    $user->save();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thnx!! that worked..! Btw I have used protected $protected = array(); which is counterpart of $fillable still why it didn't worked?
Actually, the property you are thinking about is guarded. guarded is also covered in the mass assignment docs
1

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

1 Comment

Hi abhijith, I think it is $guarded instead of $protected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.