Hafiz Siddiq
Hafiz Siddiq

Reputation: 699

Laravel routes are not working with parameters

I have declared two laravel routes in web.php, but Facing 404 error.

I want to access route parameter values in controller and based on this selection I need to perform some processing based on parameters.

My web.php code

Route::group(
        ['namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['role:admin']],
        function () {
Route::get('transaction-verified/{user-id}/{tran-id}',['uses' => 'ForgotPassTranVerifyController@transaction_verified'])->name('transaction_verified');

});

My Controller Code

public function transaction_verified($id,$tran_id)
    {
        $user = User::find($id);
        if($user)
        {
            $user->status = 'active';
            $user->save();
            $response = $this->broker()->sendResetLink(
                $user->email
            );
            echo'<pre>'; print_r($response); exit;
            $transaction = ForgotPassFineTransaction::find($tran_id);
            if($transaction)
            {
                $transaction->status = 'valid';
                $transaction->save();
            }
            $this->message = true;
            return redirect(route('admin.transaction-verify'));
        }

here is how I am calling the route,

return '<a href="' . route('admin.transaction_verified', ['id'=> $row->id, 'tran-id'=> $row->tran_id]) . '" class="btn btn-info btn-rectangle"
                      data-toggle="tooltip" data-original-title="Edit">Send Email</a>

I want to access the route parameters and based on parameters there are some tasks need to be done. But I am facing the 404 error. I am unable to figure out what is wrong?

Upvotes: 0

Views: 1166

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 14510

There are a few problems I can see:

  • According to the Laravel docs, route parameters:

    ... should consist of alphabetic characters, and may not contain a - character. Instead of using the - character, use an underscore (_).

    So parameter names like {user-id} and {tran-id} will not work. Change them to (for example) {user_id} and {tran_id}.

  • Your route definition includes those 2 parameters in the URL:

    .../{user-id}/{tran-id}...
    

    When you generate the route using route(), you must give it 2 parameters with those same names. But your code gives it an id:

    ...['id'=> $row->id, 'tran-id'=> $row->tran_id]...
    

    You need to change that id to user-id, or considering the above problem, user_id.

Upvotes: 2

Related Questions