Nifty Matrix
Nifty Matrix

Reputation: 45

Laravel 9 pass variable from route to controller

My problem: I am currently trying to refactor some of my controllers. Doing so I found these two routes:

Route::get('/events', [EventsController::class, 'eventsList'])->name('event-list');
Route::get('/courses', [EventsController::class, 'allCoursesList'])->name('all-events');

they show different filter options in the frontend.

What I want to do:

Example Code:

Route::get('/courses', [
    'all' => 1,
    EventsController::class, 'courseList'
])->name('all-events');

so having the ability to pass a variable, in this case all to my controller. EventsController So I can check with if in my controller and handle these routes differently with only one function instead of two.

With the current solutions on StackOverflow, users are using:

'uses'=>'myController@index'

now if I try it like this:

Route::get('/courses', [
    'all' => 1,
    'uses' => 'EventsController@CourseList'
])->name('all-events');

I get the following error:

Target class [EventsController] does not exist.

Question:
What is the current, correct way, to pass a variable to a controller, from a route. In Laravel 9 and 10.

Upvotes: 1

Views: 1995

Answers (3)

lagbox
lagbox

Reputation: 50491

You can pass arbitrary data to the route as a parameter using the defaults method of Route:

Route::get('courses', [EventsController::class, 'list'])
    ->name('all-events')
    ->defaults('all', 1);

Route::get('events', [EventsController::class, 'list'])
    ->name('event-list');


public function list(Request $request, $all = 0)
{
    ...
}

There are also other ways of using the Route to pass data.

Upvotes: 4

Seyid Takele
Seyid Takele

Reputation: 1208

You can use Route Parameters to pass variable from route to controller.

// web.php
Route::get('/events/{all}', [EventsController::class, 'eventsList'])->name('event-list');

then in your controller you can access the variables

   public function eventsList(Request $request,$all){

         if($all==1){
        //your logic for handling the condition when $all =1
      }

        // the remaining condition
    }

if you have multiple parameters you can pass them like so, you can use ? for optional parameter.

// web.php
Route::get('/courses/{param_one}/{param_two?}', [EventsController::class, 'allCoursesList'])->name('all-events');

then in your controller you can access the variables

    public function allCoursesList(Request $request,$paramOne,$paramTwo){

        return $paramOne.' '.$paramTwo;
    }

to access the query parameter

// web.php
Route::get('/evets', [EventsController::class, 'allCoursesList'])->name('all-events');

if the route were /events?timeframe=0&category=1 you can access the query parameter like so

    public function allCoursesList(Request $request,$paramOne,$paramTwo){
$timeframe= $request->query('timeframe');
// do this for all the query parameters
       
    }

Upvotes: 1

D1__1
D1__1

Reputation: 1357

Laravel versions 8 and above do not automatically apply namespace prefixes. This means that when passing the class name as a string, you need to use the fully qualified class name (FQCN).

For example:

Route::get('/courses', [
    'all' => 1,
    'uses' => '\App\Http\Controllers\EventsController@CourseList'
])->name('all-events');

If it makes sense for your use case, you could also use URL parameters. For example, if your Course models belong to a Category model, you might do something like this:

Route::get('/courses/{category}', [EventsController::class, 'allCourseList');

Then in your countroller, you define the allCoursesList function like so:

public function allCoursesList(Category $category)
{
    // ... do something with $category which is an instance of the Category model.
}

Upvotes: 1

Related Questions