【问题标题】:how to route localhost with existing project id in Laravel 5.2如何在 Laravel 5.2 中使用现有项目 ID 路由 localhost
【发布时间】:2017-09-26 08:05:00
【问题描述】:

我正在使用 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');

【问题讨论】:

  • return Redirect::route(你的用户路由')
  • 但它不像 localhost:8000/project/10/users 那样生成 url
  • 请您显示您的 url 路由名称:localhost:8000/project/10/users
  • Shishir 查看我编辑的答案
  • 你的路由看起来像 Route::get('project/{id}/users', function(){ return view('projects.users'); })->name('projects.用户);对于网址:localhost:8000/project/10/users

标签: php laravel-5


【解决方案1】:

你的路由看起来像 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]);

【讨论】:

  • 这一行之后 $project->save();
  • 好的,它是像这样生成的 users?id=50 但我需要这样 localhost:8000/project/10/users 如何配置这个?
  • 你改变你的路线吗?
  • 您的路线对于创建像 localhost:8000/project/10/users 这样的 url 无效。所以改变你我回答的路线。
【解决方案2】:

为此localhost:8000/project/10/users 网址 试试这种代码

// 对于具有以下 URI 的路由:profile/{id}

return redirect()->route('profile', [$user]);

【讨论】:

  • 10 是项目 ID 而不是用户 ID。
  • 这只是您喜欢的示例更改
  • Guptha 你能根据我的控制器给我一些示例代码吗?我是 Laravel 的新手
  • 好的,就像你的 cmets 一样。但我的网址打印为 localhost:8000/users?10
猜你喜欢
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
  • 2016-10-21
  • 2016-08-31
  • 2017-11-22
  • 2017-11-06
  • 2016-04-02
  • 2019-03-31
相关资源
最近更新 更多