I cannot extract the information from Json on Laravel

Asked

Viewed 549 times

3

I get a request like this...

public function store(Request $request)
{
   return response()->json($request->all());
}

That’s the way out:

anexoPdfGuia: {}

guiaFiscal: "{"empresas_id":"40","tributo_id":4,"mes_referencia":4,"vencimento":"2018-05-10","email":"[email protected]","titulo":"teste666","tributo":{"id":4,"nome":"PIS"},"mesReferencia":{"id":4,"nome":"Abril"},"data_vencimento":"2018-05-10T03:00:00.000Z","valor":1.11}"

I need to get an attribute inside the tax tab, but I’m not getting it with json_decode.

Can someone help me?

  • You want PHP to read the Laravel result?

  • No. This $request is coming in JSON.stringify. I want to know how I dismember it, to get a parameter that’s coming inside it and Josn_decode, it’s not working...

  • How the data is really coming in the store method()?

  • Is this an ajax request? This Return sends the data to some JS?

2 answers

2

With json_decode should work, make sure you are using it the right way?

Example:

$json = '{"empresas_id":"40","tributo_id":4,"mes_referencia":4,"vencimento":"2018-05-10","email":"[email protected]","titulo":"teste666","tributo":{"id":4,"nome":"PIS"},"mesReferencia":{"id":4,"nome":"Abril"},"data_vencimento":"2018-05-10T03:00:00.000Z","valor":1.11}';
$object = json_decode($json);
echo $object->empresas_id; // vai imprimir 40
  • $json = json_decode($request); Return Response($json->empresas_id); I did so...

  • has how to var_dump the $request and show me exactly what is being sent to json_decode, and also how it is trying to access the variable?

  • If I use $requestArray = json_decode($request); Return Response()->json($requestArray); It returns me { }.

0

For what you’re showing on your return

 '{"empresas_id":"40","tributo_id":4,"mes_referencia":4,"vencimento":"2018-05-10","email":"[email protected]","titulo":"teste666","tributo":{"id":4,"nome":"PIS"},"mesReferencia":{"id":4,"nome":"Abril"},"data_vencimento":"2018-05-10T03:00:00.000Z","valor":1.11}';

If your return is being as returned above then it is already an object, just by getting the attribute, so:

 public function inicio( Request $request ){
      $retorno = json_decode( $request->input( 'dados' ) );
      return response()->json( ["mes" => $retorno->mes_referencia]  );
}

The way it is in the example I sent a direct string, it might be your case:

           $('.btn').on('click', function(){
                clique();
            });

            function clique(){
                $.ajax( {
                    url : '{{ route('dados') }}',
                    type : 'post',
                    dataType: 'json',
                    data: {
                        _token : '{{ csrf_token() }}',
                        dados : '{"empresas_id":"40","tributo_id":4,"mes_referencia":4,"vencimento":"2018-05-10","email":"[email protected]","titulo":"teste666","tributo":{"id":4,"nome":"PIS"},"mesReferencia":{"id":4,"nome":"Abril"},"data_vencimento":"2018-05-10T03:00:00.000Z","valor":1.11}'
                    },
                    success: function( data ){
                        $('.saida').text( data );
                    }
                } );
            }

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.