php 通过 Laravel 中的链接 href 传递 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41619136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:25:27  来源:igfitidea点击:

Passing id through an link href in Laravel

phplaravelparametersroutes

提问by steven

is it possible to pass id through an link href in Laravel and display that page like /projects/display/2.

是否可以通过 Laravel 中的链接 href 传递 id 并显示该页面,如 /projects/display/2。

I have this link:

我有这个链接:

<td><a href="{{ url('projects/display', $projects->id) }}" class="btn btn-info">View</a></td>

It displays the id when hovering over the link as /projects/display/2. But whenever i click on the link i get an error message of:

将鼠标悬停在链接上时,它将 id 显示为 /projects/display/2。但是每当我点击链接时,我都会收到一条错误消息:

 Sorry, the page you are looking for could not be found.

I have a view setup called projects/display, plus routes and controller.

我有一个名为项目/显示的视图设置,以及路由和控制器。

routes:

路线:

<?php


Route::group(['middleware' => ['web']], function (){

    Route::get('/', 'PagesController@getIndex');

    Route::get('/login', 'PagesController@getLogin');

    Auth::routes();

    Route::get('/home', 'HomeController@index');

    Route::get('/projects/display', 'ProjectsController@getDisplay');

    Route::resource('projects', 'ProjectsController');


});

Controller:

控制器:

<?php

namespace App\Http\Controllers;

use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;

class ProjectsController extends Controller
{

    public function index()
    {



    }


    public function create()
    {
        return view('projects.create');
    }



    public function store(Request $request)
    {
        $this->validate($request, array(

            'name' => 'required|max:200',
            'description' => 'required'
        ));

        $project = new project;

        $project->name = $request->name;
        $project->description = $request->description;

        $project->save();

         Session::flash('success', 'The project was successfully created!');

        return redirect()->route('projects.show', $project->id);


    }


    public function show()
    {
        $project = Project::all(); 

        return view('projects.show')->withProject($project);

    }


    public function edit($id)
    {
        //
    }


    public function update(Request $request, $id)
    {
        //
    }

    public function getDisplay($id){


        $project = Project::find($id);

        return view('projects/display')->withProject($project);

    }





}

回答by Alexey Mezenin

You need to change your route to:

您需要将路线更改为:

Route::get('/projects/display/{id}', 'ProjectsController@getDisplay');

And then generate URL with:

然后使用以下命令生成 URL:

{{ url('projects/display/'.$projects->id) }}

回答by Harsha Sharan

If you write route like below,

如果你像下面这样写路由,

Route::get('/projects/display/{projectId}', 'ProjectsController@getDisplay')->name('displayProject');

You can use the name 'displayProject' in the href and pass the id as Array :

您可以在 href 中使用名称“displayProject”并将 id 作为 Array 传递:

<td><a href="{{ route('displayProject', ['projects' => $projects->id]) }}" class="btn btn-info">View</a></td>

回答by Daan Meijer

What you are looking for is a parameterized route. Read more about them here: https://laravel.com/docs/5.3/routing#required-parameters

您正在寻找的是参数化路由。在此处阅读有关它们的更多信息:https: //laravel.com/docs/5.3/routing#required-parameters