在laravel中填充没有模型的表单

时间:2016-03-02 20:47:51

标签: laravel binding routes

我的申请是在竞争管理员的laravel中进行的。

我已经创造了'和'编辑'团队和球员的表格。一支球队有多名球员。

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

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

2 个答案:

答案 0 :(得分:0)

在网址中传递团队ID?

/players/create?team={teamId}

PlayersController @ create method:

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

答案 1 :(得分:0)

你可以做一个例如

的路线
// 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&#39;方法更好。但是,您可以为其安装package