给出以下格式的表格:
it('should return online equals false.', fakeAsync(() => {
appService.isOnline$.subscribe(data => {
expect(data.online).toEqual(false);
});
tick(70000);
httpMock.expectOne(url).flush(undefined, mockErrorResponse);
httpMock.verify();
}));
});
和模型:
<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>
我想使用Laravel的use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $fillable = [
'firstname',
'lastname',
];
}
函数从表单中检索所有Illuminate\Http\Request::input()
字段并从中填充模型。
我用来测试此功能的示例路由是:
person
但是,这将返回以下响应:
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');
(在表单中填充了“ firstname” =>“ John”和“ lastname” =>“ Doe”的值)
是否可以使用其对应的键(“ firstname”和“ lastname”)而不是数字键从此表单中检索数组输入,还是我必须手动指定所有键?
答案 0 :(得分:1)
更改访问输入的方式
$fields = $request->input("person");
您应该得到
array:2 [▼
"firstname" => "John"
"lastname" => "Doe"
]
使用该逻辑填充这些字段应该没有问题。如果您想在一个帖子中创建多个人,则需要在表单上使用一个额外的索引:
<form method="POST" action="">
{{ csrf_field() }}
@foreach(['firstname' => 'First Name', 'lastname' => 'Last Name'] as $key => $label)
<label for="person[0][{{ $key }}]">{{ $label }}</label>
<input name="person[0][{{ $key }}]" id="person[0][{{ $key }}]" type="text" />
@endforeach
<input type="submit" value="Send" />
</form>
然后在后端循环访问:
for($request->input("person") AS $index => $fields){
$person = new Person;
$person->fill($fields);
}
// OR
$fields = $request->input("person.0");
$person = new Person;
$person->fill($fields);
动态创建字段时,您需要一种在前端维护索引的方法,但这是另一个问题。