I am currently getting data from an external application, by using Laravel in my app.
I have created a simple webhook in my controller file, like:
public function webhook(Request $request)
{
$document = new Document();
$document->fill($request->all());
//Insert (or update existing) data in our database.
$document::updateOrCreate(
['booking_reference' => $document->booking_reference],
$document->getAttributes()
);
return response()->json('OK');
}
Now my problem is, I have to insert this data in a table. In order to do this, the received json objects should match the name of my table columns. I am able to set the json names in the external application - however, sometimes the external application add child elements to the json names.
For example, imagine this is my database:
id | booking_reference | shipment_reference
And my JSON have these:
"booking_reference" : "32000"
"shipment_reference" : "TREXOOO"
Then the data will be inserted correctly in my database. However as mentioned, the JSON objects can sometimes look like this:
"booking_reference_0" : "32000"
"shipment_reference_5" : "TREX000"
In this example, my table will not be updated, since the JSON names does not match my table names.
How can I do, so I dynamically can insert the data in my table? The data will always have the prefix of the table name like booking_reference or booking_reference_{child}
Update:
Using Erubiel's answer, I can get the columns to dynamically match the name. However, I have a basic belongsTo relation setup, but solution by Erubiel strips the $document object, and only adds the mapped columns.
$document->creator()->associate(auth()->user());
and in my model:
public function creator()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
But my JSON response does not contain any user_id or creator info:
{"booking_reference":"CDK10000","pickup_location":"Afhentingsadresse 100 2650","drop_off_location":"Leveringsadresse 200 1000","comments":"HaHA","shipment_referenec":"SBRY100000"}
If I just get the $document, before the foreach loop:
{"user_id":1,"creator":{"id":1,"name":"Oliver","email":"[email protected]","created_at":"2018-08-27 10:58:10","updated_at":"2018-08-27 10:58:10"}}
Final edit, with solution
Using Erubiel's answer, I just modified it a bit to store the new values in my existing JSON object:
foreach ($request->all() as $key => $value) {
$newKey = preg_replace("/(_?\d+)+$/", '', $key); //this generates the name of column that you need
$document->$newKey = $value;
}
Which works like a charm - output:
{"user_id":1,"booking_reference":"CDK10000","pickup_location":"Afhentingsadresse 100 2650","drop_off_location":"Leveringsadresse 200 1000","comments":"HaHA","shipment_referenec":"SBRY100000","creator":{"id":1,"name":"Oliver","email":"[email protected]","created_at":"2018-08-27 10:58:10","updated_at":"2018-08-27 10:58:10"}}
shipment_reference_XXXstrpos($bookingReferenceKey, $validBookingReferenceKey), where bookingReferenceKey would be the key returned by the json, and validbookingReferenceKey would bebooking_reference