Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing id through an link href in Laravel

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

    }





}
like image 642
steven Avatar asked Jan 12 '17 17:01

steven


People also ask

How can use URL ID in laravel?

$request->id; You can get id from POST method like this. public function getDetail(Requests $rq){ $product = Product::where('id',$rq->id)->get(); } .

How do I send my ID to a route?

You can send an ID (variable value) to another page in Laravel using the route method in the anchor tag in the view file by passing the variable to it. You have to just pass data from route to controller and after controller to another view page.

How does action pass URL in laravel?

You can simply add something to do. However using route names is much easier and better in your application. You can then use my first example. public function getIndex(Request $request) { $param1=$request->input('param1'); ... }

What is Uri in laravel?

Laravel Tutorial Index Routing in Laravel allows you to route all your application requests to their appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing.


1 Answers

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>
like image 149
Harsha Sharan Avatar answered Oct 27 '22 15:10

Harsha Sharan