我正在使用Laravel 5.2并开发项目管理应用程序。在我的应用程序中,我在项目文件夹视图文件中创建了项目new.blade.php。 new.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects.store') }}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="control-label">Name</label>
<input type="text" name="name" class="form-control" id="name" value="{{ old('name') ?: '' }}">
@if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
@endif
</div>
<div class="form-group{{ $errors->has('notes') ? ' has-error' : '' }}">
<label for="notes" class="control-label">Notes</label>
<textarea name="notes" class="form-control" id="notes" rows="10" cols="10">
{{ old('notes') ?: '' }}
</textarea>
@if ($errors->has('notes'))
<span class="help-block">{{ $errors->first('notes') }}</span>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Create</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
此表单数据保存在ProjectController
中public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3',
'notes' => 'required|min:10',
]);
$project = new Project;
$project->project_name = $request->input('name');
$project->project_notes = $request->input('notes');
$project->user_id = Auth::user()->id;
$duplicate = Project::where('project_name',$project->project_name)->first();
if($duplicate)
{
return redirect()->route('projects.index')->with('warning','Title already exists');
}
$project->save();
现在我需要在将此项目数据重定向页面保存到users.blade.php文件之后,该文件位于具有ulr项目ID的相同项目文件夹中。例如
localhost:8000/project/10/users
我该怎么做?需要一些帮助......
编辑 这是我在projectcontrolle的重定向
return redirect()->route('projects.users',[$project]);
和路线
Route::get('/users', function(){
return view('projects.users');
})->name('projects.users');
答案 0 :(得分:0)
这个localhost:8000/project/10/users
网址
试试这种代码
//对于具有以下URI的路由:profile / {id}
return redirect()->route('profile', [$user]);
答案 1 :(得分:0)
你的路线看起来像url:localhost:8000 / project / 10 / users
Route::get('project/{id}/users', function(){
return view('projects.users');
})->name('projects.users');
保存项目后, 首先,您必须找到您插入的项目ID。
$project->save();
$projectId = Project::all()->last()->id;
return redirect()->route('projects.users', ['id' => $projectId]);