【发布时间】:2016-06-15 22:13:36
【问题描述】:
我的应用程序是在 laravel 中为竞赛管理员制作的。
我在团队和玩家上有“创建”和“编辑”表单。一支球队有多名球员。
我想从团队页面链接到“创建玩家”页面。 Create Player 页面不使用模型(不绑定)。我如何仍然可以从团队页面预先填写团队的选择框?我可以绑定而不在数据库中保存记录吗?
我的路线应该是什么样的?
【问题讨论】:
我的应用程序是在 laravel 中为竞赛管理员制作的。
我在团队和玩家上有“创建”和“编辑”表单。一支球队有多名球员。
我想从团队页面链接到“创建玩家”页面。 Create Player 页面不使用模型(不绑定)。我如何仍然可以从团队页面预先填写团队的选择框?我可以绑定而不在数据库中保存记录吗?
我的路线应该是什么样的?
【问题讨论】:
例如,您可以制作路线
// teamId is optional
Route::get('player/create/{teamId?}', ['as' => 'player_create', function ($teamId = null) {
// You can of course better do this logic in a controller!
// just an example :)
// check if $teamId is null here for example
// Or whatever logic you want to grab a team by
$team = Team::find($teamId);
$teams = Team::all();
// Again.. or whatever way you want to pass your data!
return view('player.create', ['teamName' => $team->name, 'teams' => $teams, 'whatever' => 'elseyouneed']);
}]);
以你的观点的形式:
{!! Form::select('team', $teams, $teamName) !!}
由于 html 不再是核心的一部分,因此您不能开箱即用地使用它,所以我认为 Chris 的方法更好。但是,您可以为它安装 package。
【讨论】:
在 URL 中传递团队 ID?
/players/create?team={teamId}
PlayersController@create 方法:
$teams = Team::all();
return view('players.create', compact('teams'));
players.create查看:
<select name="team">
@foreach ($teams as $team) {
<option value="{{ $team->id }}"{{ $request->has('team') && $request->query('team') === $team->id ? ' selected' : '' }}>{{ $team->name }}</option>
@endforeach
</select>
【讨论】: