Only update field if form value exists
Create Base model and override update function like
/**
* @param array $attributes
* @return mixed
*/
public function update(Array $attributes = array()){
foreach($attributes as $key => $value){
if(!is_null($value)) $this->{$key} = $value;
}
return $this->save();
}
After use:
$model = Model::find($id);
$model->update(Input::only('param1', 'param2', 'param3'));
Using Input::only('foo', 'bar')
will grab only the values needed to complete the request - instead of using Input::all()
.
However, if 'foo' or 'bar' doesn't exist within the input, the key will exist with the value of null
:
$input = Input::only('foo', 'bar');
var_dump($input);
// Outputs
array (size=2)
'foo' => null
'bar' => null
To filter in a clean way, any values with a null
value:
$input = array_filter($input, 'strlen');
In your example, this would replace: $account->fill(Input::all());