简体   繁体   English

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

[英]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对于发布请求,无需在 url 中传递参数。您将进入请求

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:如果您不想更改您的 url,请在您的控制器apiPaymentByUserId()方法中尝试此操作,注入 Request 对象以及其他路径变量,例如:

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 .对于POST请求,无需在 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).将日期作为通过 POST 方法发送的 FORM 值与其余 FORM 值一起发送(如果有,您已经在 FORM 中发布)。 You will get all FORM values sent via POST method in Request $request object instance, passed in Controller/Method .您将获得通过请求 $request对象实例中的 POST 方法发送的所有 FORM 值,并在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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