在laravel中填充不带模型的表单

在laravel中填充不带模型的表单,laravel,binding,routes,Laravel,Binding,Routes,我的申请是在拉雷维尔为一个竞赛管理员 我有“创建”和“编辑”球队和球员的表格。一个队有多个队员 我想从团队页面链接到“创建玩家”页面。创建播放器页面不使用模型(不绑定)。如何仍然使用团队页面中的团队预先填充选择框?我可以绑定而不在数据库中保存记录吗 我的路线应该是什么样的?在URL中传递团队ID /players/create?team={teamId} PlayersController@create方法: $teams = Team::all(); return view('player

我的申请是在拉雷维尔为一个竞赛管理员

我有“创建”和“编辑”球队和球员的表格。一个队有多个队员

我想从团队页面链接到“创建玩家”页面。创建播放器页面不使用模型(不绑定)。如何仍然使用团队页面中的团队预先填充选择框?我可以绑定而不在数据库中保存记录吗


我的路线应该是什么样的?

在URL中传递团队ID

/players/create?team={teamId}
PlayersController@create方法:

$teams = Team::all();

return view('players.create', compact('teams'));
玩家。创建
视图:

<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>

@foreach($teams作为$team){
已选择('team')&&$request->query('team')===$team->id':''}}}>{{{$team->name}}
@endforeach

例如,您可以创建一条路线

// 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的方法更好。但是,您可以为它安装一个