doulu1544
2017-01-12 17:12
浏览 110
已采纳

通过Laravel中的链接href传递id

is it possible to pass id through an link href in Laravel and display that page like /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:

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

    }





}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzhao4071 2017-01-12 17:15
    已采纳

    You need to change your route to:

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

    And then generate URL with:

    {{ url('projects/display/'.$projects->id) }}
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhanbi0027 2017-01-12 17:15

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

    评论
    解决 无用
    打赏 举报
  • douchuopiao9002 2019-06-20 07:54

    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 :

    <td><a href="{{ route('displayProject', ['projects' => $projects->id]) }}" class="btn btn-info">View</a></td>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题