0

I have a little moment that I don't understand because I'm new to development. I'm collecting data from multi step form and want to handle all form inputs at one ajax request on my controlle. I did it successfully, but can't figure out how to use data from $request if it's an array from ajax. I can use if it $request->input('name') but in my case I need something like $request->input('firstGrant.issue_date') because of my data format. Please tip me which way to dig.

My method:

submitCompany() {
 axios.post('/onboarding', {
    name: this.step1.name,
    type: this.step1.type,
    shares_amount: this.step2.shares_amount,
    par_value: this.step2.par_value,
    firstGrant: {
        issue_date: this.step3.firstGrant.issue_date,
        certificate: this.step3.firstGrant.certificate,
        share_price: this.step3.firstGrant.share_price,
        shares_amount: this.step3.firstGrant.shares_amount
    }

})
.then(function (response) {
    console.log(response);
    alert('Information saved!');
})
.catch(function (error) {
    console.log(error);
    alert('Wrong!');
});
}

My Controller:

public function store(Request $request)
{
    $userId = Auth::user()->id;
    $issueDate = $request->input('firstGrant.issue_date'); //How to do it right way?
    $certificate = $request->input('firstGrant.certificate');//How to do it right way? 
    $sharePrice = $request->input('firstGrant.share_price');//How to do it right way?
    $sharesAmount = $request->input('firstGrant.shares_amount');//How to do it right way?

    $equityGrant = EquityGrant::create([
        'user_id' => $userId,
        'share_id' => 91,
        'vesting' => 0,
        'status' => 'active',
        'issue_date' => $issueDate,
        'certificate' => $certificate,
        'share_price' => $sharePrice,
        'shares_amount' => $sharesAmount,
    ]); }

1 Answer 1

2

You might have to configure axios to send a header along with every request that will make Laravel recognize the request as being XHR. Once it does, the $request->input('x.y') statements should work.

Object.assign(axios.defaults.headers, {
    'X-Requested-With': 'XMLHttpRequest',
});

If this still does not work you might also want to check whether axios properly includes the CSRF-token in a request header.

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

2 Comments

Hey, thanks for a tip. Should I past this code just before axios.post()? P.S. I have CSRF tokens.
Works! Thanks dude!

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.