Aogiri14
Aogiri14

Reputation: 65

How to pass post parameters in laravel from the route to the controller?

I'm doing this query in the payment controller and i need to get a post request from the route.

Controller:

class PaymentController extends Controller 
{
    public function apiPaymentByUserId($date_from, $date_to) { 


        $payments = DB::table("casefiles, payments")
            ->select("payments.*")
            ->where("casefiles.id", "=", 'payments.casefile_id')
            ->where("casefiles.user_id", "=", Auth::id())
            ->where("payments.created_at", ">=", $data_from)
            ->where("payments.updated_at", "<=", $data_to)
            ->get();

            
            return response()->json([ 
                'success' => true, 
                'response' => $payments 
            ]);
    
    }
}

Route:

Route::post('/payments/{date_from}/{date_to}', 'Api\PaymentController@apiPaymentByUserId');

How to pass multiple parameters in this post route? Thank you

Upvotes: 1

Views: 2391

Answers (3)

Ronnie Depp
Ronnie Depp

Reputation: 159

For POST request no need to pass param in url . Send the Dates as FORM values sent via POST method along with the rest of the FORM values (if any, you're already POSTing in the FORM). You will get all FORM values sent via POST method in Request $request object instance, passed in Controller/Method.

So route will be:

Route::post('/payments', 'Api\PaymentController@apiPaymentByUserId');

and controller method:

public function apiPaymentByUserId(Request $request) 
{ 
    $date_from = $request->date_from;
    $date_to = $request->date_to;
}

Upvotes: 0

John Lobo
John Lobo

Reputation: 15339

For post request no need to pass param in url .You will get in request

So route will be

Route::post('/payments', 'Api\PaymentController@apiPaymentByUserId');

and controller method

public function apiPaymentByUserId(Request $request) 
{ 
    $date_from = $request->date_from;
    $date_to = $request->date_to;
}

Upvotes: 5

Hisham
Hisham

Reputation: 411

If you do not want to change your url, try this in your controller apiPaymentByUserId() method, inject the Request object along with the other path variables like like:

public function apiPaymentByUserId(Illuminate\Http\Request $request, $date_from, $date_to) { 
      // ... you can access the request body with the methods available in $request object depending on your needs.
}

Upvotes: 2

Related Questions