简体   繁体   中英

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

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

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.
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM