1

I have an external api that send a POST to my server the POST body comes in a json response as an array

{
  "IDs": [
    1378143,​1378141,​1971403,​1451025,​1451026,​1374561,​1378036,​1371423,​1371424,​         
    1971568,​1371426,​1718539,​1718515,​1831163,​1718540,​1362068,​1376341,​1378221,
    1378222,​1378220,​1380468,​1380467,​1417322,​1385612,​1971567
  ] 
}

I save the post as following:

$Id = new IdModel();
$Id->id = $request['IDs'];
$Id->save();

This save the array in my database column id as "Array" how can I get all the values in the array to save instead of the word Array?

4
  • You ought use stackoverflow's snippet code tool to show the JSON response. It might look better Commented Jul 27, 2016 at 14:31
  • you want to put all the id's, numbers in one entry, or each in a different entry ? ? Commented Jul 27, 2016 at 14:34
  • You might want to foreach($request['IDs'] as $id) {} to process each ID separately. It's unclear from your question if each ID should get a separate IdModel record or if something else is going on. Commented Jul 27, 2016 at 14:39
  • Yeah the final solution would be to have each ID it's separate IdModel record I know how I can pass those into each record. I am just trying to understand why the array displays as "Array" in the column of the db. Commented Jul 27, 2016 at 15:55

4 Answers 4

2

Use attribute casting. Open your IdModel model and add this:

protected $casts = [
    'id' => 'array'
];

This will convert the array every time you get it from the DB and every time you save it to the DB.

Sign up to request clarification or add additional context in comments.

5 Comments

Note: this requires the column to have an appropriate data type. JSON, text, etc. - not integer or a string column too small to fit the data.
I changed the column to have a type JSON as well as added the cast to the id column
@TheFallen I haven't received any post from the API in the post few days so I am waiting on that to see if it worked
@TheFallen yes this is working now by changing the data type to JSON Thanks
@Kyl great, please mark the question as resolved if this solved your problem.
0
$Id = new IdModel(); 
$IDs = '';
foreach($request['IDs'] as $id) {
    $IDs.= ',' . $id;
}
$Id->id = $IDs; 
$Id->save();

Your content inside IDs property is an array, you should use foreach to iterate your array and concatenate every ID to save it on your DB.

If you want to create a row for every ID, you should use:

foreach($request['IDs'] as $id) {
    $newId = new IdModel();
    $newId->id = $id; 
    $newId->save();
    unset($newId);
}

2 Comments

implode would be better than foreach in this case, but I think it's highly unlikely $Id->id is supposed to be a comma-separated list of IDs.
Added your case to my answer, thanks for the feedback.
0

Just add your code in loop

foreach($request['IDs'] as $id) {
    $obj = new IdModel();
    $obj->id = $id;
    $obj->save();
}

Comments

0

I changed the data type to JSON as @TheFallen had suggested and it works now. Thanks everybody for the help and input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.