Laravel Collection does n't work correctly - php

I have a problem with my collection
$tasks = Bear::all();
$val = new Collection([]);
foreach($tasks as $tas){
$val ->push($tas->id);
}
$tasks1 = BearPic::whereIn('bear_id',$val)->get();
$val2 = new Collection([]);
foreach($tasks1 as $tas){
$val ->push($tas->bear_id);
}
my echo on val one is
[10,11,12,10,10,11,11,12,12]
And on tasks1 is
[{
"id": 1,
"bear_id": 10,
"picnic_id": 1,
"created_at": null,
"updated_at": null
}, {
"id": 2,
"bear_id": 10,
"picnic_id": 2,
"created_at": null,
"updated_at": null
}, {
"id": 3,
"bear_id": 11,
"picnic_id": 1,
"created_at": null,
"updated_at": null
}, {
"id": 4,
"bear_id": 11,
"picnic_id": 2,
"created_at": null,
"updated_at": null
}, {
"id": 5,
"bear_id": 12,
"picnic_id": 1,
"created_at": null,
"updated_at": null
}, {
"id": 6,
"bear_id": 12,
"picnic_id": 2,
"created_at": null,
"updated_at": null
}]
the second collection returns empty
what might be the problem? I am rather new to laravel this is a tutorial

The second collection val2 is empty simply because you are pushing the values to val i.e. the first collection instead of val2.
Also, if you insist on not using joins and working on Collections, pluck is indeed a quicker solution.
$tasks = Bear::all();
$val = $tasks->pluck('id');
$tasks1 = BearPic::whereIn('bear_id', $val)->get();
$val2 = $tasks2->pluck('bear_id');

If I understand correctly, you wanted to put all of id values of $tasks1 collection to $val2, am I correct? If so then you could simply do like this:
$tasks1 = BearPic::whereIn('bear_id',$val)->get();
$val2 = $tasks1->pluck('id');

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.

Access to a sub-property in Laravel 7

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.

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.

Laravel use join to fetch data return null

in this below result of fetch data:
{
"purchases": [
{
"id": 1,
"member_id": 1,
"mediator_id": 2,
"type": null,
"cash": 38712402,
"created_at": "2017-11-08 06:40:45",
"updated_at": "2017-11-08 06:40:45",
"deleted_at": null,
"cheques": [
{
"id": 1,
"purchase_id": 1,
"expire_date": "1396-01-19",
"amount": 97094060,
"isRejected": 0,
"created_at": "2017-11-08 06:40:45",
"updated_at": "2017-11-08 06:40:45",
"deleted_at": null
}
]
},
{
"id": 2,
"member_id": 1,
"mediator_id": 2,
"type": null,
"cash": 46760191,
"created_at": "2017-11-08 06:40:45",
"updated_at": "2017-11-08 06:40:45",
"deleted_at": null,
"cheques": []
}
]
}
which i get that by this code:
$purchases = \App\Purchase::with('cheques')->where('member_id',
$request->userId)->get();
i'm trying to get amount from cheques and 'cash','created_at' from purchases where 'member_id'=$request->userId, but i get empty cheques when i use this code:
$purchases = \App\Purchase::with(array('cheques'=>function($query){
$query->select('amount');
}))->where('member_id',$request->userId)->get(['cash','created_at']);
Can you try this?
$purchases = \App\Purchase::with(array('cheques'=>function($query){
$query->select('id', 'amount', 'purchase_id');
}))->where('member_id',$request->userId)->get(['cash','created_at']);
Try the Eager Loading Specific Columns part from the documentation- https://laravel.com/docs/5.5/eloquent-relationships
It says:
When using this feature, you should always include the id column in the list of columns you wish to retrieve.
$purchases = \App\Purchase::with(
array('cheques' => function($query){
$query->select('amount');
})
)->where('member_id',$request->userId)
->select('cash','created_at')->get();
If you want to select columns using get methon of collection, you need to pass id:
->get(['id', 'cash', 'created_at']); or you can use ->select('cash', 'created_at')->get();
Which you prefer.

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