lundi 23 décembre 2019

laravel save json to database mysql

I have a problem saving a json result to a table in the mysql database.

Json Return

{ "retorno": {
  "status_processamento": 3,
    "status": "OK",
    "pagina": "1",
    "numero_paginas": "1",
    "pedidos": [
      {
        "pedido": {
          "id": 123456,
          "numero": 123456,
          "numero_ecommerce": "12",
          "data_pedido": "01/01/2013",
          "data_prevista": "10/01/2013",
          "nome": "Cliente Teste",
          "valor": "100.25",
          "id_vendedor": "123456",
          "nome_vendedor": "Vendedor Teste",
          "situacao": "Atendido"
        }
      },
      {
        "pedido": {
          "id": 123456,
          "numero": 123458,
          "numero_ecommerce": "15",
          "data_pedido": "01/01/2013",
          "data_prevista": "10/01/2013",
          "nome": "Cliente Teste 3",
          "valor": "50.25",
          "id_vendedor": "",
          "nome_vendedor": "",
          "situacao": "Aberto"
        }
      }
    ]
  }
}

Model

namespace App;

use Illuminate\Database\Eloquent\Model;
class Rating extends Model
{

 protected $fillable = [

          'ratingKey'

      ];

}

Controller

  $data = json_decode($response, true);

  Rating::create(
  [ 'ratingKey'  => $data['retorno']['pedidos'][0]['pedido']['id']   ]);

this code works perfectly but only saves one answer from json and i would like to save all the answers and if i change the code to

'ratingKey'  => $data['retorno']['pedidos'][]['pedido']['id'] 

I get this error

Symfony\Component\Debug\Exception\FatalErrorException Cannot use [] for reading



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire