如何将laravel中的post参数从路由传递到控制器



我在支付控制器中进行此查询,需要从路由中获得一个post请求。

控制器:

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::post('/payments/{date_from}/{date_to}', 'ApiPaymentController@apiPaymentByUserId');

如何在此后路线中传递多个参数?谢谢

对于post请求,无需在url中传递param。您将获得请求

所以路线将是

Route::post('/payments', 'ApiPaymentController@apiPaymentByUserId');

和控制器方法

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

如果你不想更改你的url,请在你的控制器apiPaymentByUserId()方法中尝试,注入Request对象和其他路径变量,比如:

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

对于POST请求,无需在url中传递参数。将Dates作为通过POST方法发送的FORM值与其他FORM值一起发送(如果有,则表示您已经在FORM中进行了POST(。您将在Request$Request对象实例中通过POST方法发送所有FORM值,并在Controller/method中传递。

所以路线将是:

Route::post('/payments', 'ApiPaymentController@apiPaymentByUserId');

和控制器方法:

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

相关内容

  • 没有找到相关文章