Access to a sub-property in Laravel 7 - php

on Laravel I have an answer for this from a query:
{
"id": 19,
"skin_id": 13,
"skin_payment_provider_id": 16,
"payment_method_id": 24,
"rank": 1,
"is_visible": 1,
"is_enabled": 1,
"created_at": "2020-07-16T10:09:51.000000Z",
"updated_at": "2020-07-16T10:09:51.000000Z",
"deleted_at": null,
"laravel_through_key": 13,
"payment_provider": {
"id": 5,
"parent_id": null,
"name": "Paypal",
"slug": "paypal",
"thumb": null,
"is_enabled": true,
"options": null,
"percentage": null,
"rank": 1,
"created_at": "2020-07-16T08:47:49.000000Z",
"updated_at": "2020-07-16T08:47:49.000000Z",
"deleted_at": null,
"laravel_through_key": 16
}
}
I can access the properties without problems such as:
$result->payment_method_id or $result->skin_id etc but I can't access the property $result->payment_provider!
if I call a return $result I see the answer I put above but if I call $result->payment_provider I have an empty answer.
I tried to call through Arr::get() but it is always the same...
What am I missing?
Thanks

Not sure how you convert the json to an array..
i guess your payment provider is converted as an array an not an object
$result->payment_provider['id']; or $result['payment_provider']['id']
Dumping it like dd($result)
or print_r($result)
Will help you understand whats in your var.

Related

Laravel paginate(10) not working for second page

I am trying Laravel's paginate function but the URL below returns the same records every time. This means that it's showing only the first 10 records no matter which page we're on. I don't know why.
URL - https://example.com/api/getWebGalleryImages?page=3
My Controller function
public function getWebGalleryImages() {
$galleryFIle = WebGallery::orderBy('id', 'desc')->paginate(10);
return response()->json($galleryFIle, 200);
}
My route
Route::get('getWebGalleryImages', 'FrontController#getWebGalleryImages');
Every time same response
{
"current_page": 1,
"data": [
{
"id": 11,
"file_name": "1627985668_download(2).jpg",
"created_at": null,
"updated_at": null
},
{
"id": 10,
"file_name": "1627985646_istockphoto-1093301674-612x612.jpg",
"created_at": null,
"updated_at": null
},
{
"id": 9,
"file_name": "1627985646_images(4).jpg",
"created_at": null,
"updated_at": null
},
{
"id": 8,
"file_name": "1627985646_images(3).jpg",
"created_at": null,
"updated_at": null
},
{
"id": 7,
"file_name": "1627985645_images(2).jpg",
"created_at": null,
"updated_at": null
},
{
"id": 6,
"file_name": "1627985645_images(1).jpg",
"created_at": null,
"updated_at": null
},
{
"id": 5,
"file_name": "1627985645_download(2).jpg",
"created_at": null,
"updated_at": null
},
{
"id": 4,
"file_name": "1627985645_download(1).jpg",
"created_at": null,
"updated_at": null
},
{
"id": 3,
"file_name": "1627985644_Diamond.jpg",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"file_name": "1627985644_depositphotos_3929711-stock-photo-diamond-on-black-background.jpg",
"created_at": null,
"updated_at": null
}
],
"first_page_url": "https://example.com/api/getWebGalleryImages?page=1",
"from": 1,
"last_page": 2,
"last_page_url": "https://example.com/api/getWebGalleryImages?page=2",
"next_page_url": "https://example.com/api/getWebGalleryImages?page=2",
"path": "https://example.com/api/getWebGalleryImages",
"per_page": 10,
"prev_page_url": null,
"to": 10,
"total": 11
}
From the Response in your Question, your query seems to have 11 matched records and the your Result Per Page is set to 10 meaning you only have 2 pages of records. so calling page=3 in your query is not going to get you a new record as that page is unavailable.
If you need the pagination to work set your Result Per Page to a lower value, let say 3 or add more data to the records.

'Trying to get property 'value_type1' of non-object' in laravel

I have the following query running in a function:
$sensor = Sensor::where('id', $measurement->sensor_id)->with('sensorType', 'sensorType.valueType1', 'sensorType.valueType2')->first();
This returns the following response:
{
"id": 1,
"name": "DHT22 Kantoor",
"sensor_type_id": 1,
"active": 1,
"organisation_id": 1,
"disabled_until": null,
"created_at": null,
"updated_at": "2020-07-15T11:12:33.000000Z",
"sensor_type": {
"id": 1,
"name": "DHT22",
"value_1_type_id": 1,
"value_2_type_id": 2,
"created_at": null,
"updated_at": null,
"value_type1": {
"id": 1,
"name": "Temperatuur",
"unit": "°C",
"created_at": null,
"updated_at": null
},
"value_type2": {
"id": 2,
"name": "Luchtdruk",
"unit": "Pa",
"created_at": null,
"updated_at": null
}
}
I am trying to get the 'Temperatuur' that is in this response in the following way:
$affValue = $sensor->sensor_type->value_type->name;
But this gives me the following error:
Trying to get property 'value_type1' of non-object
I've tried doing the following:
$sensor->name returns DHT22 Kantoor
$sensor->sensor_type->id gives the same error as before
Am I trying to access this value wrong? Or is there a diffrent solution?
I got it working now.
Instead of using sensor_type i needed to use sensorType
The same for value_type1, it needed to be valueType1
So instead of using the name that's in the response you need to use the name that is in the App\SensorType.

Laravel/Lumen: Handling the fetchresults of Model::With('Relation')

I have three related tables:
Table Coretable which is referenced by Table extensiontable_itc and extensiontable_sysops.
I now want to get all the records from coretable which are referenced by extensiontable_itc and extensiontable_sysops, and to do so, I run this code:
$join = coretable::with('extensiontable_itc','extensiontable_sysops')->get();
and this kind of works, I get the following result:
[{
"id": 1,
"Internal_key": "TESTKEY_1",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 1,
"coretable_id": 1,
"description": "EXTENSION_iTC_1",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 1,
"coretable_id": 1,
"description": "EXTENSION_SYSOPS_1"
}
}, {
"id": 2,
"Internal_key": "TESTKEY_2",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 4,
"coretable_id": 2,
"description": "EXTENSION_ITC_2",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 2,
"coretable_id": 2,
"description": "EXTENSION_SYSOPS_2"
}
}, {
"id": 3,
"Internal_key": "TESTKEY_3",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 5,
"coretable_id": 3,
"description": "EXTENSION_ITC_3",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 3,
"coretable_id": 3,
"description": "EXTENSION_SYSOPS_3"
}
}, {
"id": 4,
"Internal_key": "TESTKEY_4",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 6,
"coretable_id": 4,
"description": "EXTENSION_ITC_4",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 4,
"coretable_id": 4,
"description": "EXTENSION_SYSOPS_4"
}
}, {
"id": 5,
"Internal_key": "TESTKEY_5",
"created_at": null,
"updated_at": null,
"extensiontable_itc": {
"id": 7,
"coretable_id": 5,
"description": "EXTENSION_ITC_5",
"created_at": null,
"updated_at": null
},
"extensiontable_sysops": {
"ID": 5,
"coretable_id": 5,
"description": "EXTENSION_SYSOPS_5"
}
}]
As you can see, the records referencing the respective record on coretable are put into an array, which itself becomes an element of the overall array containing the data from coretable.
Now, I want to get ALL this data EXCEPT for:
The ids from coretable
The created_at/updated_at
the foreign keys/coretable_ids
I already worked with the collection method "pluck()", but it doesn't seem to fit the purpose of accessing this multi-level array with all these duplicates in the indexes.
Is there any convenient way to do this? Or do I have to do it manually, using loops?
https://laravel.com/docs/5.8/eloquent-serialization#hiding-attributes-from-json
There are two ways to hide fields. The first one is to put needed fields to $hidden array inside model class, and all these fields will not be included in the model instance. The second one is to use makehidden function. This allows you to hide fields dynamically, depend on some conditions as an example.

How to modify array structure returned by eloquent in laravel

I currently have the below array structure that I get when running Bonus::with('users')->get()
I have tried adding the array and merging similar keys but it is not working. I tried using the helper methods provided in laravel but I don't seem to be able to get it working.
[
{
"id": 1,
"users_id": 1,
"bonus_type_id": 1,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 2,
"users_id": 1,
"bonus_type_id": 2,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 3,
"users_id": 2,
"bonus_type_id": 2,
"is_paid": 1,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "another test",
"email": "another#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
}
]
I want the result to be something like that
[
{
user_id: 1,
name: "test",
bonuses: [
{
bonus_type_id: 1,
amount: 100,
is_paid: 0,
},
{
bonus_type_id: 2,
amount: 100,
is_paid: 0,
}
],
},
{
user_id: 2,
name: "another",
bonuses: [
{
bonus_type_id: 2,
amount: 100,
is_paid: 1,
},
]
}
]
You need to change your eloquent relationship. Define hasMany relationship function bonuses inside User model for bonuses table and use that to get the desired result like so:
User::with('bonuses')->get();
That way it will return results from users table and populate bonuses for each user.
makes sense?
You can get specific information with select subquery, if your relations are correctly set, this should work.
User::select('user_id','name','bonus_id')
->with(array('bonuses'=> function($query){
$query->select('bonus_type_id','amount','is_paid');
}))->get();

Merge two objects, while one is inside of an array

I need to merge 2 objects using PHP (Laravel) one of them is in an array, the other is a plain object, but also contains a nested object, is there any simple way to do it?
Edit: The result should be a single object
[
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"state_id": 1,
"type_id": 7,
"created_at": "2016-11-18 11:39:19",
"updated_at": "2016-11-18 11:39:19",
"deleted_at": null
}
]
and
{
"id": 1,
"parent_id": null,
"name": "Chrysler",
"state_id": 1,
"type_id": 1,
"created_at": "2016-11-18 11:39:10",
"updated_at": "2016-11-18 11:39:10",
"deleted_at": null,
"pivot": {
"vehicle_id": 1,
"brand_id": 1
}
}
Desired output should be:
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"name": "Chrysler",
}
It has to be something similar, otherwise it would break a lot of JavaScript code connected to the first object.
<?php
//Merge the two as an array
$merged = array_merge($array,(array)$object);
//As an object
$merged = (object)$merged;

Categories