How to send variable by href to route and received by controller

Viewed 317

This is my Blade

<a data href="/Detail/{{$FT->Folder_ID}}">
Link
</a>

This is my Route

Route::get('/Detail/{{$FT->Folder_ID}}','DetailController@index');

and this is my cotroller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

 class DetailController extends Controller
 {
    //
    public function index($request){
        dd($request);
    }
 }

i was wondering, if i can send the variable(folder_ID) so the controller can do some function with it

4 Answers

There is an error in your route :

Route::get('/Detail/{folder_ID}','DetailController@index');

And in the index method :

public function index($folder_ID){
    dd($folder_ID);
}

Find below code snippets. This could work fine with your requirement.

Your Blade Code:

<a data href="/Detail/{{$FT->Folder_ID}}"> Link </a> 

Your Corrected Route Code:

Route::get('/Detail/{{$id}}','DetailController@index');

Your Controller :

<?php

 namespace App\Http\Controllers;
 use Illuminate\Http\Request;

 class DetailController extends Controller
 {
    public function index($id){
     dd($id);
    }
  }

For sending a variable concatenate this to your url:

www.xyz.com/controller_name?id=Folder_ID

In the above url Folder_ID is a variable and will dynamically fetch the value from the link that is clicked.

now you can use the variable id in the controller using :

$_GET['id'];

Hope it helps

Make it simple and clear. Following is the example how i use it. give name to your route like this ROUTE

Route::get('/tutor_approval/{id}','DashboardController@tutor_approval')->name('tutor_approval');

Now your anchor tag in something.blade.php. use route name in anchortag VIEW

<a href="{{route('tutor_approval',['id' => 'tutor_id'])}}">approve</a>

Controller

public function tutor_approval($id){
        $values = array('admin_approval' => 1);
        $update = Tutor::where('tutor_id', $id)->update($values);
        return \Response::json(array('success' => true), 200);
}
Related