0

I'm using Laravel. When I dd($request->all()) it prints data only when the array is not empty i.e. Array['some-value']. When I console.log(data) in jQuery, it logs in console that it is an array but just doesn't have any value inside. However in Laravel I can't get that empty array, but I can get it when it has value. I want to send array even if it's empty.

How can I do that?

UPDATE

HTML:

<ul>
    <!-- It sends but doesn't show in Laravel when this active class below is not exists -->
    <li class="active" data-motion="1">motion 1</li>
    <li data-motion="2">motion 2</li>
    <li data-motion="3">motion 3</li>
</ul>

jQuery:

$.each($('ul li.active'), function(index, motion) {
    motion = $(motion).attr('data-motion');
    params['motions'].push(motion);
});

// it shows the empty array when that active class is not there,
// which is the one I want to get in Laravel too.
consol.log(params);

$.ajax({
    url: '/my-motions',
    type: 'PUT',
    dataType: 'json',
    data: params,
    success: function(response) {
        params = [];
    },
    error: function(error) {
        //
    }
});

Laravel:

public function myFunc(Request $request)
{
    $data = $request->all();
    dd($data);
    // The result: nothing
    // What I want: array:0 [] 
}
7
  • Show your relevant code, both PHP and JS. Commented Jan 28, 2019 at 6:54
  • 1
    Just check if the request is empty in if condition. Refer stackoverflow.com/questions/42230304/… Commented Jan 28, 2019 at 6:58
  • What's the output of dd($request->all());? Commented Jan 28, 2019 at 7:38
  • the output is [] but i have lots of other key/value pairs. the result when not sending the param is [['some-key' => 'some-value'], ['some-key2' => 'some-value2'], ['motions' => ['1','2','3']]] and what I want is [['some-key' => 'some-value'], ['some-key2' => 'some-value2'], ['motions' => []]] Commented Jan 28, 2019 at 7:46
  • try passing data:{motion:motionArr} Commented Jan 28, 2019 at 7:55

0

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.