0

I need to pass those parameters data_from and data_to to the axios call but it's not working. I'm doing something wrong? The response is undefined.

Controller:

class PaymentController extends Controller 
    {
        public function apiPaymentByUserId(Request $request) { 
    
    
            $data_from = $request -> data_from;
            $data_to = $request -> data_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', 'Api\PaymentController@apiPaymentByUserId');

Axios call with vue js:

axios.post('/payments', {
    
    params: {

        data_from: this.data_from,
        data_to: this.data_to

    }
})

3 Answers 3

2

Your axios code is

axios.post('/payments', {
    
    params: {

        data_from: this.data_from,
        data_to: this.data_to

    }
})

if you dd($request->all()) in controller you will get below output

array:1 [
  "params" => array:2 [
    "data_from" => ""
    "data_to" => ""
  ]
]

so to fetch individual like below

$request->params['data_from']

So better pass data in axios like below

axios.post('/payments', {

        data_from: this.data_from,
        data_to: this.data_to
})

so you can fetch

$request->data_from

Ref: https://github.com/axios/axios

Sign up to request clarification or add additional context in comments.

Comments

1

Change that

            $data_from = $request -> $data_from;
            $data_to = $request -> $data_to;

for this

            $data_from = $request->data_from;
            $data_to = $request->data_to;

Comments

0

try accessing route parameters like this $data_from = $request->data_from;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.