Vie-6ren">
gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:41:06 24 4
gpt4 key购买 nike

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

我有这个链接:

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

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

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

我有一个名为 projects/display 的 View 设置,以及路由和 Controller 。

路线:

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

}





}

最佳答案

您需要将路线更改为:

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

然后生成 URL:

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

关于php - 通过 Laravel 中的链接 href 传递 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41619136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com