How to access a property value inside JsonResponse Object in PHP or Laravel?

前端 未结 3 1899
迷失自我
迷失自我 2021-01-12 19:46

I\'m doing a POST using Ajax and my Server is getting the data just fine. However, I\'m struggling to access the value the user sent. In simple words how can I access the va

相关标签:
3条回答
  • 2021-01-12 20:05

    Using Laravel the data can also be accessed using illuminate getData() method.

    $someVar->getData();
    

    https://laravel.com/api/5.3/Illuminate/Http/JsonResponse.html#method_getData

    0 讨论(0)
  • 2021-01-12 20:09

    I solve my issue and I'm going to share it in case someone needs it. So the way I was getting the JsonObjec was by doing this in Routes.php:

    Route::post('/register', function(){
    if(Request::ajax()){
        Log::info('From Ajax: ' . print_r(Response::json(Request::all()), true));
    
        return var_dump(Response::json(Request::all()));
    } 
    });
    

    But instead I did this to actually access the value of user (Tom).

    $somevar = (Request::all());
    Log::info('From Ajax: ' . print_r($somevar["user"], true));
    

    This solve my issue. Hope it helps anyone out there!

    0 讨论(0)
  • 2021-01-12 20:24

    With Laravel you can access to JSON data same way as regular variables. In your case you need something like:

    $username = $request->get('user');
    
    0 讨论(0)
提交回复
热议问题