0

I am using Laravel 4.x. Is it possible to pass Input:all() rather than setting individual properties of object and then calling save()?

My form fields are similar to db fields in naming convention.

Volatil3
  • 14,253
  • 38
  • 134
  • 263

2 Answers2

1

From laravel documentation you have options like -

$user = User::create(array('name' => 'John'));

// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(array('name' => 'John'));

// Retrieve the user by the attributes, or instantiate a new instance...
$user = User::firstOrNew(array('name' => 'John'));

You can use all these as following

$user = User::create(Input::all());

// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(Input::all());

// Retrieve the user by the attributes, or instantiate a new instance...
$user = User::firstOrNew(Input::all());

But you need to be careful that your form field name and database column names are same.

Also you have to look for $guarded on your model. Those field will not able to be inserted this way.

0

You can use Model::create(Input::all())

For this to work, you need to specify a protected $fillable array in the model, which specifies mass-assignable fields.

protected $fillable=array('column1','cloumn2');
jayant
  • 1
  • 1