Volatil3
Volatil3

Reputation: 14988

Is there anyway to use Input::all() to create a new record?

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.

Upvotes: 0

Views: 319

Answers (2)

jayant
jayant

Reputation: 1

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

Upvotes: 0

Shimul Chowdhury
Shimul Chowdhury

Reputation: 191

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.

Upvotes: 1

Related Questions