I can not extract information from Json in Laravel

3

I get a request like this ...

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

The output is this:

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 guide, but I'm not getting it with json_decode.

Can anyone help me?

    
asked by anonymous 11.05.2018 / 14:55

2 answers

2

With json_decode should work, are you sure you're using it correctly?

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
    
11.05.2018 / 15:06
0

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 returned as above then it is already an object, all you have to do is get the attribute, like this:

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

As 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.

    
11.05.2018 / 17:52