I am using Eloquent Relationship in Laravel to output my data to JSON. When I use Eloquent Relations they show up inside JSON as an object but I need only JSON output with variables inside without object or json, As shown below:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
},
Instead of this (the result for the relation one to many toJSON in LARVEL) :
$activity = Activity::find(1)->load('history')->toJSON();
result:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history": [
{
"id": 1,
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
}
]
},
Any ideas how this can be done ? I can do it using DB join (), but is there a way to do it using Eloquent? Besides :
$activity = \App\Models\Activity::find(1)
->join('activity_history', 'activity_history.id', '=',
'activities.history_id')->select('*')->get()
->toJSON();
Because this is not the best way to do it, because I am using the column names and the table names, which I have to go to the DB behind to look at them
You can around doing this by flattening the array when you get that result. Let's imagine you $object->history to get the result with the history relationship, and simply do:
Arr::dot($object->history);
So you'll get an array like:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history.id": 1,
"history.type_id": 2,
"history.comment": "Horoshiy Specialist",
"history.result_id": 2,
"history.result_comment": "Prowerili anketu i obrazowanie",
"history.start_time": null,
"history.end_time": null,
}
Related
I would like to add an object into a existing json object :
My existing json object :
{
"id": 1,
"horaire_id": 1,
"json": "{"i": "idHoraire1-NumReservation1", "x": 3, "y": 2, "w":5, "h": 3, "j": 0}",
"num_reserv": 1,
"nombre_heures": 7.5,
"created_at": "2021-09-16T13:55:00.000000Z",
"updated_at": "2021-09-16T13:55:03.000000Z"
}
And the object to insert into the existing json object :
{
"id": 1,
"evenement_type_id": 1,
"reservation_id": 1,
"posinreserv": 1,
"campus_id": 1,
"comment": null,
"created_at": null,
"updated_at": null
}
We tried json_encode and json_decode, without success :
$reservation = json_decode($reservation, true)
All this with Laravel 7 (not is js)
Thank you.
$data1 = '{
"id": 1,
"horaire_id": 1,
"json": "{"i": "idHoraire1-NumReservation1", "x": 3, "y": 2, "w":5, "h": 3, "j": 0}",
"num_reserv": 1,
"nombre_heures": 7.5,
"created_at": "2021-09-16T13:55:00.000000Z",
"updated_at": "2021-09-16T13:55:03.000000Z"
}';
$data2 = '{
"id": 1,
"evenement_type_id": 1,
"reservation_id": 1,
"posinreserv": 1,
"campus_id": 1,
"comment": null,
"created_at": null,
"updated_at": null
}';
Laravel
$array = array_merge($data1->toArray(), $data2->toArray());
PHP
$array = array_merge(array($data1), array($data2));
You can use the spread operator: (note json objects are just arrays in php, assuming you've json_decoded it)
$result = [...$array_1, ...$array_2]
or
$result = array_merge($array_1, $array_2)
I'm getting a bit confused when retrueving card information. I get the card data like this:
card_data = customer.sources.list(limit=3, object='card')
card = card_data['data']
print(card)
And this is what is printed:
[<Card card id=card_1DPKYtAuBx2mXUsrmQG0gHMz at 0x54d8420> JSON: {
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": "42424",
"address_zip_check": "pass",
"brand": "Visa",
"country": "US",
"customer": "cus_Dqyu8HKCIQnUIA",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 4,
"exp_year": 2024,
"fingerprint": "TuMlU4wS6zLVngGc",
"funding": "credit",
"id": "card_1DPKYtAuBx2mXUsrmQG0gHMz",
"last4": "4242",
"metadata": {},
"name": null,
"object": "card",
"tokenization_method": null
}]
I'm unable to parse this data as if it were normal json "card['id']". How do I go about getting data from the fields?
The card you got from card = card_data['data'] is an array. You could get the id by card[0]['id']
Give it a try
Thanks
For the first time I'm trying to use unit tests for my projects, but I'm blocked with a test asserting that a model is properly stored.
This is the API controller I want to test:
public function store(QuestionFormRequest $request)
{
$questionRequest = $request->question();
$question = new Question($questionRequest);
$question->save();
$question->answers()->createMany($questionRequest['answers']);
return response()->json($question->load('answers'), 201);
}
This is my test:
public function it_can_store_a_question()
{
$surveyFactory = factory(Survey::class)->create();
$themeFactory = factory(Theme::class)->create();
$pillarFactory = factory(Pillar::class)->create();
$questionToStore = [
'survey_id' => $surveyFactory->id,
'theme_id' => $themeFactory->id,
'pillar_id' => $pillarFactory->id,
'name' => 'question',
'type' => 'simple',
'answers' => [
[
'label' => 'reponse1',
'points' => '3',
],
[
'label' => 'reponse2',
'points' => '5',
]
]
];
$response = $this->post('/api/1.0/question', $questionToStore);
$response->assertStatus(201);
$expectedQuestion = Question::with('answers')->get()->first();
$this->assertEquals(json_encode($expectedQuestion), $response->getContent());
}
This is the result:
Failed asserting that two strings are equal.
Expected :'{"id":1,"survey_id":1,"theme_id":1,"pillar_id":1,"name":"question","type":"simple","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null,"answers":[{"id":1,"question_id":1,"label":"reponse1","points":3,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null},{"id":2,"question_id":1,"label":"reponse2","points":5,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null}]}'
Actual :'{"survey_id":1,"theme_id":1,"pillar_id":1,"name":"question","type":"simple","updated_at":"2017-08-29 08:54:45","created_at":"2017-08-29 08:54:45","id":1,"answers":[{"id":1,"question_id":1,"label":"reponse1","points":3,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null},{"id":2,"question_id":1,"label":"reponse2","points":5,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null}]}'
In fact, the result is right. But not in the same order.
What am I doing wrong in my test?
Thanks.
For your example you could use assertions shipped with phpunit/phpunit:
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonString()
assertJsonStringEqualsJsonFile()
or
assertEquals()
assertJsonFileEqualsJsonFile()
However, when I - for the sake of an example - extract the two JSON strings into separate files
expected.json
actual.json
and then format the JSON in PhpStorm with option + command + L and run the following test
use PHPUnit\Framework;
final class JsonTest extends Framework\TestCase
{
public function testJsonEquals()
{
$expected = __DIR__ . '/expected.json';
$actual = __DIR__ . '/actual.json';
$this->assertJsonFileEqualsJsonFile($expected, $actual);
}
}
the test fails with
Failed asserting that '{\n
"id": 1,\n
"survey_id": 1,\n
"theme_id": 1,\n
"pillar_id": 1,\n
"name": "question",\n
"type": "simple",\n
"created_at": "2017-08-29 08:54:45",\n
"updated_at": "2017-08-29 08:54:45",\n
"deleted_at": null,\n
"answers": [\n
{\n
"id": 1,\n
"question_id": 1,\n
"label": "reponse1",\n
"points": 3,\n
"description": "",\n
"created_at": "2017-08-29 08:54:45",\n
"updated_at": "2017-08-29 08:54:45",\n
"deleted_at": null\n
},\n
{\n
"id": 2,\n
"question_id": 1,\n
"label": "reponse2",\n
"points": 5,\n
"description": "",\n
"created_at": "2017-08-29 08:54:45",\n
"updated_at": "2017-08-29 08:54:45",\n
"deleted_at": null\n
}\n
]\n
}\n
' matches JSON string "{
"survey_id": 1,
"theme_id": 1,
"pillar_id": 1,
"name": "question",
"type": "simple",
"updated_at": "2017-08-29 08:54:45",
"created_at": "2017-08-29 08:54:45",
"id": 1,
"answers": [
{
"id": 1,
"question_id": 1,
"label": "reponse1",
"points": 3,
"description": "",
"created_at": "2017-08-29 08:54:45",
"updated_at": "2017-08-29 08:54:45",
"deleted_at": null
},
{
"id": 2,
"question_id": 1,
"label": "reponse2",
"points": 5,
"description": "",
"created_at": "2017-08-29 08:54:45",
"updated_at": "2017-08-29 08:54:45",
"deleted_at": null
}
]
}
".
--- Expected
+++ Actual
## ##
{
- "id": 1,
"survey_id": 1,
"theme_id": 1,
"pillar_id": 1,
"name": "question",
"type": "simple",
+ "updated_at": "2017-08-29 08:54:45",
"created_at": "2017-08-29 08:54:45",
- "updated_at": "2017-08-29 08:54:45",
- "deleted_at": null,
+ "id": 1,
What you can see is that in fact the two JSON strings aren't equal, at least the order of the fields isn't the same. The problem with the output of the assertion is that it's not really helpful, simply because the assertion just compares the two strings.
assertEquals()
Nonetheless, when we json_decode() the content of the files and then assert using assertEquals() like this:
use PHPUnit\Framework;
final class JsonTest extends Framework\TestCase
{
public function testJsonEquals()
{
$expected = json_decode(file_get_contents(__DIR__ . '/expected.json'), true);
$actual = json_decode(file_get_contents(__DIR__ . '/actual.json'), true);
$this->assertEquals($expected, $actual);
}
}
then the test fails again, but with output that is actually much more helpful:
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
## ##
- 'deleted_at' => null
As you can see, there's an actual difference between the data - the root object node is missing a deleted_at field with a value of null.
Note So, the question boils down to
do you want to assert that two JSON strings are equal or
do you want to assert that the data represented by two JSON strings are equal
Depending on which is important to you, pick whichever assertion makes more sense to you.
The JSON output is just a little bit different - instead of comparing the actual string output you'd be better to use one of Laravel's JSON assertions to ensure the output contains what you expect. Take a look at the available assertions in the documentation. You might use $response->assertJson() or $response->assertJsonFragment() to ensure the required items are returned.
I'm using PgSQL9.6.
I have procedure named as createBooking in PgSQL. It receive few parameters and one of them is additional type text.
Request is
{
"email": "user2#name.com",
"start_date": "2016-10-21 11:00:00+00",
"end_date": "2016-10-21 15:00:00+00",
"guests": 50,
"firstname": "User",
"lastname": "Name",
"payment_type": "Method 1",
"phone": "+871253875123",
"user_id": 22,
"y_id": 3,
"additionals": [
{
"extrasId": 6,
"money": 19750,
"packageId": null,
"amount": 3
},
{
"extrasId": 11,
"money": 64380,
"packageId": null,
"amount": 5
},
{
"extrasId": null,
"money": null,
"packageId": 3,
"amount": 1
}
]
}
I'm trying fetch 'additioanl'.
FOR i IN SELECT json_array_elements(createBooking.additional)
LOOP
sum := sum + i->>'money';
END LOOP;
It does not mater what is in body of loop.
When I calling this function via PostMan it is sending me response with error.
{
"hint": "No function matches the given name and argument types. You might need to add explicit type casts.",
"details": null,
"code": "42883",
"message": "function json_array_elements(text) does not exist"
}
Can some body explain me how should I fetch it?
Thank you.
I have a Session object I want to model in Ember data. (actually display sessions but it's the same)
The JSON from the server looks like this (cannot be changed):
{
"metadata": {
"page": 1,
"page_size": 100,
"total_num_objects": 7,
"total_num_pages": 1
},
"result": [
{
"api_path": "/rest/sessions/2",
"end_time": 1412687629.42063,
"hostname": "127.0.0.1",
"id": 2,
"logical_id": "c6656738-4e23-11e4-9017-685b35b63131_0",
"product_name": null,
"product_revision": null,
"product_version": null,
"start_time": 1412687629.26851,
"status": "SUCCESS",
"type": "session",
"user_name": null
},
{
"api_path": "/rest/sessions/3",
"end_time": 1412688377.15329,
"hostname": "127.0.0.1",
"id": 3,
"logical_id": "84707366-4e25-11e4-a659-685b35b63131_0",
"product_name": null,
"product_revision": null,
"product_version": null,
"start_time": 1412688377.11507,
"status": "SUCCESS",
"type": "session",
"user_name": null
},
...
I realize I need to write a custom RESTSerializer but I can't figure out what do I need to do in order go get rid of metadata + make ember realize that result is actually a session.
Side question:
Can I make the DS.Model.extend attributes like what I get from the API or do I must use CamelCase and use normalizeHash
You'd probably need to do something like this:
App.SessionSerializer = DS.RESTSerializer.extend({
normalizePayload: function(payload) {
return {
sessions: payload.result
};
}
});
See: http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_normalizePayload
Regarding your other issue, look at DS.ActiveModelAdapter / ActiveModelSerializer. ActiveModelSerializer handles the underscore convention in the JSON.