0

I am making an AJAX post to Laravel controller like below.

<script>
    let data = {
       a: true,
       b: false
    }

    this.axios.post(this.url, data)
      .then (result => console.log(result) )
      .catch (result => console.log(result) )
</script>

On the backend side in the Laravel 5.6 controller post action method, I am trying to check what values are posted by AJAX call from frontend. And then I log it to laravel log file.

<?php

   public function myPostHandler(Request $request) {
         $data = $request->all();
        \Log::info("Posted data = " . print_r($data, true));
   }
?>

In the laravel.log file ... it shows that the values are empty like below -

[a] =>
[b] => 

So my question is how should I send the boolean data to controller so that it is not getting lost? And also like to understand why Laravel is behaving this way and why my boolean values are getting trimmed ? Is it something caused by middleware ?

PS: Below is my routes which does not have any issue:

 Route::get('something/{catchall}', [
            'uses' => 'myController@index'
        ])->where('catchall', '(.*)');

 Route::post('something/{catchall}', [
            'uses' => 'myController@update'
        ])->where('catchall', '(.*)');
Andy
  • 2,493
  • 6
  • 37
  • 63
  • I am not doing any specific handling of data in the middleware BTW. – Andy Jun 14 '18 at 00:22
  • I have passed boolean values in my past projects, can you show me your route? there must be something that happens before reaching your controller – Ikong Jun 14 '18 at 00:54
  • Edited my question to add routes. But routes is not an issue. – Andy Jun 14 '18 at 02:07
  • I made it work by using a work-around of using text values for true ==> 'true' and false ==> 'false'. But I am still not sure why I can not just simply post boolean true or false to laravel controller action? – Andy Jun 14 '18 at 02:09
  • What does the request parameters show under the network tab in chrome devtools? – Derek Pollard Jun 14 '18 at 02:34
  • 1
    Take a look at [https://stackoverflow.com/a/43471980/3003285](https://stackoverflow.com/a/43471980/3003285) – Aryan Jun 14 '18 at 02:44

1 Answers1

1

In methods "myPostHandler" received data is ok. I gues \Log::info write boolean type with cast to string/ Try convert data before log

    $data = $request->all();
    foreach($data as $key => $elem){
        if (is_bool($elem)) {
            $data[$key] = ($elem) ? 'true' : 'false';
        }
    }
    \Log::info("Posted data = " . print_r($data, true));
Vitalii
  • 41
  • 4