API in Laravel receive POST JSON and write to database

Asked

Viewed 3,410 times

4

I will receive through the API a JSON with this structure:

{"leads":
  [{"id":"1",
    "email":"[email protected]",
    "name":"Bruno Ghisi",
    "company":"Resultados Digitais",
    "job_title":"IT",
    "bio":"This is my bio",
    "created_at":"2012-06-04T15:31:35-03:00",
    "opportunity":"false",
    "number_conversions":"3",
    "first_conversion": {
      "content": {
        "identificador":"ebook-abc",
        "nome":"Bruno",
        "email_lead":"[email protected]",
        "telefone":"99999999",
        "empresa":"Resultados Digitais",
        "cargo":"IT"
      },
      "created_at":"2012-06-04T15:31:35-03:00",
      "cumulative_sum":"1",
      "source":"source 1"
    },
    "last_conversion": {
      "content": {
        "identificador":"webinar-abc",
        "email_lead":"[email protected]"
      },
      "created_at":"2012-06-04T15:31:35-03:00",
      "cumulative_sum":"2",
      "source":"source 2"
    }
  }]
}

My API should receive this data and record in my database, I am starting the use of Laravel and followed a tutorial of Vedovelli, the API is receiving and recording Posts performed without being in json format but qnd will test in this format it inserts a line into the database recording only the ID without the other information.

My Controller is like this:

public function saveLead()
    {
        return Response::json($this->lead->saveLead(), 200);
    }

and Model in this way:

 public function saveLead()
    {
        $input = Input::all(); //pega todos os dados do input
        $lead = new Lead(); //criar um novo registro
        $lead->fill($input); //inclui todos os dados do input
        $lead->save(); //salva o input
        return $lead; //retorna o lead gravado
    }

If I add a dd($input); it shows me an array with the data, but at the time of writing does not insert the values in the database.

From now on I thank those who can help me. :/

  • your bd structure is following the same return json? names including case-sensitives

  • From what I understand, in the same action of a controller you can receive a request both "normal" and JSON. Is that right? Your action has to be smart enough to know if the coming Request contains JSON or not?

1 answer

4


I managed to solve it this way:

public function saveConversion()
    {
        $input = file_get_contents('php://input'); // Pega todos os dados do json
        $jsonDecode = json_decode($input); // Decodifica o json e transforma em objeto
        $leads = $jsonDecode->leads[0];

        $lead->id = $leads->id;
        $lead->email = $leads->email;
        $lead->name = $leads->name;
        $lead->company = $leads->company;
        $lead->job_title = $leads->job_title;
        $lead->bio = $leads->bio;
        $lead->opportunity = $leads->opportunity;
        $lead->number_conversions = $leads->number_conversions;

        $lead->save(); // Salva o input

        return $lead; // Retorna o lead gravado
    }

Browser other questions tagged

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