Mass Assignment in Laravel 4

A wonderful feature of Ruby on Rails and also coincidentally in Laravel 4 is mass assignment. Basically you can throw an array of values at your model and insert or update an entry from a form.

In Laravel 3 you had a method called fill which was called on your model object like this: $user = new User; $user->fill($array_values); – in Laravel 4 the method has been replaced with create instead.

The below example will touch upon validation based on rules defined in your model and is based on a fictitious blog application.

Mass Assignment model example

The Controller

		`passes() ) 				{ 					$post = new Post; 					$post::create(Input::all()); 					$post->save();  					return Redirect::to('post/'.$post->id); 				} 				else 				{ 					View::make('public.post.add'); 				} 			}  		}`

The Model

		 `'required|min:5', 				'description' => 'required', 			);  		}`

In a nutshell that is mass assignment using the create method of the Model object instance. We don’t go into depth regarding validation and controllers as it is assumed you already are aware how those work.