Given a form in the format:
<form method="POST" action="{{ route('post') }}">
{{ csrf_field() }}
@foreach([
'firstname' => 'First name',
'lastname' => 'Last name',
] as $key => $label)
<label for="person[{{ $key }}]">{{ $label }}</label>
<input name="person[{{ $key }}]" id="person[{{ $key }}]" type="text" />
@endforeach
<input type="submit" value="Send" />
</form>
And a model:
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $fillable = [
'firstname',
'lastname',
];
}
I would like to use Laravel's Illuminate\Http\Request::input() function to retrieve all the person fields from the form and fill the model from it.
The example route that I used to test this functionality is:
use Illuminate\Http\Request;
use App\Person;
Route::post('/testcase', function(Request $request) {
$person = new Person;
$fields = $request->input('person.*');
$person->fill($fields);
var_dump(
$person->firstname,
$person->lastname,
$fields
);
return response('');
})->name('post');
However, this returns the following response:
NULL
NULL
array(2) {
[0]=>
string(4) "John"
[1]=>
string(3) "Doe"
}
(Where the form was filled with the values, "firstname" => "John" and "lastname" => "Doe")
Is it possible to retrieve the array inputs from this form with their corresponding keys ("firstname" and "lastname") instead of numerical keys, or will I have to specify all the keys manually?