I get data from database using this query
$cats = Category::with(['cat_trans' => function($q) use($lang){
$q->where('lang_code', $lang);
}])->with(['cat_prod' => function($query) use ($lang,$currency){
$query->with(['pro_trans' => function ($q) use ($lang){
$q->where('lang_code', $lang);
}]);
////////////
$query->with(['pro_price' => function ($q) use ($currency){
$q->with('currency_code')->where('cur_code', $currency);
}]);
///////////
}])->whereHas('account_type', function($qr) use ($account_type){
$qr->where('account_type_id', $account_type);
})->get();
I'm trying to remove the empty objects from the result, when I tried this I got the following response
{
"1": {
"id": 1,
"parent_id": null,
"order": 1,
"name": "Moblie",
"slug": "mobile-1",
"created_at": "2018-07-08 09:41:08",
"updated_at": "2018-07-08 10:30:17",
"cat_trans": [
{
"id": 1,
"category_id": 1,
"field": "title",
"value": "Mobile",
"lang_code": "en",
"created_at": "2018-07-08 09:51:59",
"updated_at": "2018-07-08 09:51:59"
},
{
"id": 2,
"category_id": 1,
"field": "desc",
"value": "smart",
"lang_code": "en",
"created_at": "2018-07-08 09:52:41",
"updated_at": "2018-07-08 09:52:41"
},
{
"id": 12,
"category_id": 1,
"field": "slug",
"value": "mobile-1",
"lang_code": "en",
"created_at": null,
"updated_at": null
}
],
}
}
I want to remove the key "1" from all the responses.
I used unset to get this value using this code
foreach ($cats as $k) {
if (count($k->cat_prod) == 0) {
unset($cats[$va]);
}
$va++;
}
Then I tried using array_values but it displays that I can just use array_values on an array not an object.
You can remove key "1" by simply doing
array_values((array)$cats)
You were receiving error for this function like "Can Only be used on array" was due to Laravel Query returns object , so you just need to do type conversion on it.
$cats->toArray() if u need either to convert object to array or convert with some specific conditions
Your output looks like JSON so you can do this to eliminate the 1:
json_encode( $cats->{1} );
You can use laravel simple method mapWithKeys()
$cats = $cats->mapWithKeys(function ($item) {
return $item > 2;
});
$keyed->all();
Related
I am new in laravel in php. So it might be very silly mistake. I have song table and song categories table. I am trying to fetch all category with their respective songs. I have implemented larvel eloquent one to many relationship between song category and song.
Here is my code of fetching data:
public function getSongCategoriesWithSongs(){
$json_array = array();
$song_categories = SongCategory::all();
foreach ($song_categories as $item) {
# code...
$json = [];
$json['category'] = $item;
$json['songs'] = $item->songs;
array_push($json_array,$json);
}
return $json_array;
}
Here is response:
[{
"category": {
"id": 1,
"title": "Rock",
"created_at": "2020-12-20T02:58:32.000000Z",
"updated_at": "2020-12-20T02:58:32.000000Z",
"songs": [{
"id": 1,
"title": "Mere Mehboob",
"thumbnail": "https:\/\/static.toiimg.com\/photo\/msid-71407401\/71407401.jpg?108311",
"song_category_id": 1,
"stream_link": "https:\/\/2u039f-a.akamaihd.net\/downloads\/ringtones\/files\/mp3\/mere-mehboob-qayamat-hogi-52150.mp3",
"created_at": "2020-12-20T13:26:30.000000Z",
"updated_at": "2020-12-20T13:26:30.000000Z"
}, {
"id": 2,
"title": " Taaron Ke Shehar",
"thumbnail": "https:\/\/static.toiimg.com\/photo\/msid-71407401\/71407401.jpg?108311",
"song_category_id": 1,
"stream_link": "https:\/\/newmp3ringtones.net\/assets\/sass\/Ringtones\/TaaronKeSheharRingtoneByNehaKakkarJubinNautiyal2145436126.mp3",
"created_at": null,
"updated_at": null
}, {
"id": 3,
"title": "Bewafa Tera Masoom Chehra",
"thumbnail": "https:\/\/static.toiimg.com\/photo\/msid-71407401\/71407401.jpg?108311",
"song_category_id": 1,
"stream_link": "https:\/\/newmp3ringtones.net\/assets\/sass\/Ringtones\/BewafaTeraMasoomChehraRingtoneByJubinNautiyal352778308.mp3",
"created_at": null,
"updated_at": null
}]
}
}, {
"songs": [{
"id": 1,
"title": "Mere Mehboob",
"thumbnail": "https:\/\/static.toiimg.com\/photo\/msid-71407401\/71407401.jpg?108311",
"song_category_id": 1,
"stream_link": "https:\/\/2u039f-a.akamaihd.net\/downloads\/ringtones\/files\/mp3\/mere-mehboob-qayamat-hogi-52150.mp3",
"created_at": "2020-12-20T13:26:30.000000Z",
"updated_at": "2020-12-20T13:26:30.000000Z"
}, {
"id": 2,
"title": " Taaron Ke Shehar",
"thumbnail": "https:\/\/static.toiimg.com\/photo\/msid-71407401\/71407401.jpg?108311",
"song_category_id": 1,
"stream_link": "https:\/\/newmp3ringtones.net\/assets\/sass\/Ringtones\/TaaronKeSheharRingtoneByNehaKakkarJubinNautiyal2145436126.mp3",
"created_at": null,
"updated_at": null
}, {
"id": 3,
"title": "Bewafa Tera Masoom Chehra",
"thumbnail": "https:\/\/static.toiimg.com\/photo\/msid-71407401\/71407401.jpg?108311",
"song_category_id": 1,
"stream_link": "https:\/\/newmp3ringtones.net\/assets\/sass\/Ringtones\/BewafaTeraMasoomChehraRingtoneByJubinNautiyal352778308.mp3",
"created_at": null,
"updated_at": null
}]
}, {
"category": {
"id": 2,
"title": "Soft",
"created_at": null,
"updated_at": null,
"songs": []
}
}, {
"songs": []
}]
As you can see songs get repeated.
UPDATE
Solved using eager loading
public function getSongCategoriesWithSongs(){
return SongCategory::with('songs')->get();
}
But don't know why the foreach method not working.
Try this code
public function getSongCategoriesWithSongs(){
$json_array = array();
$song_categories = SongCategory::all();
foreach ($song_categories as $item) {
$json_array[] = ['category' => $item, 'songs' => $item->songs] ;
}
return $json_array;
}
The problem is that you assign the same relation twice.
Each SongCategory already has a collection of songs inside.
So in your foreach block, you assign a category with $json['category'] => $item which will load all related songs and pass them to the final JSON object. And you duplicate this by passing the next item $json['songs'] = $item->songs to the same array. Default Laravel behavior will be to fetch all related objects and transform them into JSON.
I would suggest you to use Laravel resources to return JSON objects with exact shapes: API Resources.
You can fix your code block without eager loading by removing $json['songs'] = $item->songs assignment.
Eager loading works because you passed all your objects only once.
I have a project in laravel which has API. I'm asking the API for posts(I call them recommendation).
Now my api response look like this -
{
"success": true,
"data": {
"current_page": 1,
"data": [
{
"id": 3,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
}
...
...
...
All is good but I don't wanna likes_count to be in the result. It is a relation method. I get from it all I need. It is if the user liked this post. And it is is_liked in the result . but likes_count automatically added to the response .
if($item->likesCount->contains($user->id)){
$item['is_liked']='true';
}
I tried delete it with
foreach ($recommendations as $item) {
unset($item['likes_count']);
}
But it doesn't do it.
I think the problem is how you are referencing the object data structure. You are attempting to unset() something that doesn't exist so no error is thrown but the likes_count isn't getting removed either.
Here's your code example (fixed and modified for demonstration):
<?php
$apiResult = <<<eod
{
"success": true,
"data": {
"current_page": 1,
"data": [
{
"id": 3,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres",
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
},
{
"id": 4,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres",
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
}
]
}
}
eod;
$result = json_decode($apiResult);
$data = $result->data->data;
var_dump($data);
// This should remove the likes_count array from the $result structure.
foreach ($data as &$item) {
unset($item->likes_count);
}
var_dump($data);
A quick way would be to make that 'attribute' (relation) hidden:
$recommendations->makeHidden('likes_count');
Though I am not sure how you are building your response.
I am using Laravel 5.7 to build an API that provides a JSON response. I am creating the following JSON but it needs some changes. Table contain booking_pics columns in which multiple images stored using , separated. I want fetch in json as object in array. i am display json but only last image is display others is not, need solution.
Controller:
$get_booking_details= DB::table('table_booking_list')
->join('table_booking_details', 'table_booking_list.booking_id', '=', 'table_booking_details.booking_id')
->select('table_booking_details.*')
->where('table_booking_details.booking_id',$booking_id)
->get();
foreach($get_booking_details as $item)
{
foreach(explode(",",$item->booking_pics) as $items)
{
$item->booking_pics=[["image" => $items]];
}
}
return response()->json(['success' => '1','data' =>$get_booking_details]);
json response:
{
"success": "1",
"data": [
{
"id": 1,
"booking_list_id": 1,
"booking_id": 1,
"booking_name": "hockey stadium",
"booking_area": "kolhapur",
"booking_status": 0,
"time": "6.00 am to 8.00pm",
"booking_pics": [
{
"image": "http://192.168.1.132:8000/images/ground_pic/2.jpg"
}
],
"available_sports": "hockey,cricket",
"booking_amenities": "parking,toilet,water",
"booking_rating": 4.5,
"booking_area_address": "MSEB Ring Road, Datta Colony, Kolhapur, Maharashtra, 416008",
"longitude": "85.501980",
"latitude": "23.624420",
"updated_at": "2019-06-26 16:42:02",
"created_at": "0000-00-00 00:00:00"
}
]
}
Required json:
{
"success": "1",
"data": [{
"id": 1,
"booking_list_id": 1,
"booking_id": 1,
"booking_name": "hockey stadium",
"booking_area": "kolhapur",
"booking_status": 0,
"time": "6.00 am to 8.00pm",
"booking_pics": [{
"image": "http://192.168.1.132:8000/images/ground_pic/1.jpg"
},
{
"image": "http://192.168.1.132:8000/images/ground_pic/2.jpg"
}
],
"available_sports": "hockey,cricket",
"booking_amenities": "parking,toilet,water",
"booking_rating": 4.5,
"booking_area_address": "MSEB Ring Road, Datta Colony, Kolhapur, Maharashtra, 416008",
"longitude": "85.501980",
"latitude": "23.624420",
"updated_at": "2019-06-26 16:42:02",
"created_at": "0000-00-00 00:00:00"
}]
}
Do it this way
foreach($get_booking_details as $item)
{
foreach(explode(",",$item->booking_pics) as $items)
{
$item->booking_pics["image"][] = $items;
}
}
fastest way would be creating a model and casting the data from database as a json
in model
protected $casts = [
'booking_pics' => 'json',
];
I have a single array of data, I want to add a key and it's value in same array . Here in addedPost I want to add key favouritePost and it's value is $favouritePost after product key. How can i do this ?
Here is my query:
$addedPost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereId($postData['post_id'])
->first();
$favouritePost = PostFavourite::isAlreadyAdded($postData['post_id'], Auth::id());
return [
'status_code' => $status_code,
'message' => $message,
'PostDetails' => $addedPost
];
What I get in response :
{
"PostDetails": {
"id": 289,
"user_id": 12,
"product_id": 2,
"demand_or_supply": "Demand",
"description": "edited1",
"status": "Expired",
"created_at": "2018-06-22 07:35:27",
"updated_at": "2018-07-05 06:42:56",
"product": {
"id": 2,
"title": "Diamond",
"icon": null,
"status": "Active"
}
}
}
EXPECTED RESULT:
{
"PostDetails": {
"id": 289,
"user_id": 12,
"product_id": 2,
"demand_or_supply": "Demand",
"description": "edited1",
"status": "Expired",
"created_at": "2018-06-22 07:35:27",
"updated_at": "2018-07-05 06:42:56",
"product": {
"id": 2,
"title": "Diamond",
"icon": null,
"status": "Active"
},
"favouritepost": {
"id": 8,
"post_id": 289,
"user_id": 12
}
}
}
First: Your $addedPost is not an array but a Eloquent Collection. There are multiple possibilites to do this. The easiest one is to union an Array with the Collection.
$union = $addedPost->union($favouritePost->toArray());
For every other solution please take a look at the Laravel Documentation. It's pretty easy to understand.
https://laravel.com/docs/5.6/collections
Edit: Though I missed the ->first() inside the question just use the solution already mentioned. ->first() returns a StdClass Object, so you can handle it like it:
$addedPost->favouritepost = $favouritePost;
That property favouritePost is added to $addedPost object in that case. There's no need for any method call again.
first you store all the values which you are returning in one associative array then use array_push() to add new key/value in that array.After all you just return that array.
Assuming that $addedPost is a laravel collection you can simply do:
$addedPost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereId($postData['post_id'])
->first();
$favouritePost = PostFavourite::isAlreadyAdded($postData['post_id'], Auth::id());
$addedPost->put('favouritepost', $favouritePost);
return [
'status_code' => $status_code,
'message' => $message,
'PostDetails' => $addedPost
];
for adding key value in object
$object->new_key="value";
I am trying to list entries in a table by Month, Year:
May, 2015
Item 1
Item 2
Item 3
June, 2015
Item 1
etc..
I have achieved this with the following code but I would also like to paginate the results. I have tried many different things but none of them seem to work, I am using Laravel 5.
$events = App\Events->orderBy('start', 'asc')->get()->groupBy(function($date) {
return $date->start->format('F, Y');
});
Here is the output for the above query:
{
"April, 2015": [
{
"id": "10",
"event_type_id": "1",
"user_id": "1",
"title": "Testing",
"slug": "testing",
"start": "2015-04-23 17:00:00",
"end": "2015-04-23 17:40:00",
"description": "<h1>MEETING!</h1><p>Let's try this in HTML!<br></p>",
"created_at": "2015-04-19 14:18:33",
"updated_at": "2015-04-21 22:07:41",
"type": {
"id": "1",
"name": "General",
"slug": "general",
"created_at": "2015-04-18 11:24:00",
"updated_at": "2015-04-18 11:24:04"
}
}
],
"May, 2015": [
{
"id": "12",
"event_type_id": "1",
"user_id": "1",
"title": "Test Event",
"slug": "test-event",
"start": "2015-05-15 18:00:00",
"end": null,
"description": "<p>This is a test event with just a start time</p>",
"created_at": "2015-04-21 14:59:56",
"updated_at": "2015-05-02 18:37:53",
"type": {
"id": "1",
"name": "General",
"slug": "general",
"created_at": "2015-04-18 11:24:00",
"updated_at": "2015-04-18 11:24:04"
}
},
{
"id": "9",
"event_type_id": "1",
"user_id": "1",
"title": "Monthly Meeting",
"slug": "monthly-meeting",
"start": "2015-05-23 14:00:00",
"end": "2015-04-16 20:00:00",
"description": "<p>It's a long monthly meeting!</p>",
"created_at": "2015-04-19 13:13:45",
"updated_at": "2015-05-03 08:45:56",
"type": {
"id": "1",
"name": "General",
"slug": "general",
"created_at": "2015-04-18 11:24:00",
"updated_at": "2015-04-18 11:24:04"
}
}
],
"June, 2015": [
{
"id": "11",
"event_type_id": "1",
"user_id": "1",
"title": "Another Meeting Saved",
"slug": "another-meeting-saved",
"start": "2015-06-19 18:00:00",
"end": null,
"description": "<p>It's another meeting afterall</p>",
"created_at": "2015-04-20 15:03:30",
"updated_at": "2015-05-03 08:46:19",
"type": {
"id": "1",
"name": "General",
"slug": "general",
"created_at": "2015-04-18 11:24:00",
"updated_at": "2015-04-18 11:24:04"
}
}
]
}
With LengthAwarePaginator -
$paginator = new LengthAwarePaginator($events, count($events), 1);
return $paginator;
This returns the paginator but the data is the same - meaning the same result set as without the paginator, when I'd expect only one record to be returned per page:
[{
"total": 3,
"per_page": 1,
"current_page": 1,
"last_page": 3,
"next_page_url": "/?page=2",
"prev_page_url": null,
"from": 1,
"to": 3,
"data": {
"data" : ".. same as above"
}
}]
With aggregates you need to implement your own custom paginator, as stated by docs:
Note: Currently, pagination operations that use a groupBy statement
cannot be executed efficiently by Laravel. If you need to use a
groupBy with a paginated result set, it is recommended that you query
the database and create a paginator manually.
See this posts to manually implement pagination:
Laravel 5 - Manual pagination
Manually Creating a Paginator (Laravel 5)
Many people have pointed me to a widely mentioned paragraph in the Laravel documentation,
Note: Currently, pagination operations that use a groupBy statement
cannot be executed efficiently by Laravel. If you need to use a
groupBy with a paginated result set, it is recommended that you query
the database and create a paginator manually.
Not terribly helpful, since I cannot find any example in the documentation as to exactly how to create a manual paginator using the results of an Eloquent query. So, here is what I was able to come up with. Note that you must use ->take() and ->offset() in the query, otherwise you will end up with the same results on every page (this is where I was getting stuck).
<?php
// routes.php
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
get('test', function(Request $request) {
$page = $request->has('page') ? $request->input('page') : 1; // Use ?page=x if given, otherwise start at 1
$numPerPage = 2; // Number of results per page
$eventType = EventType::find(1); // Not relevant to pagination
$count = $eventType->memberEvents()->count(); // Get the total number of entries you'll be paging through
// Get the actual items
$events = $eventType->memberEvents()->orderBy('start', 'asc')
->take($numPerPage)->offset(($page-1)*$numPerPage)->get()->groupBy(function($date) {
return $date->start->format('F, Y');
});
// Create the paginator with Illuminate\Pagination\LengthAwarePaginator as Paginator
// Pass in the variables supplied above, including the path for pagination links
$paginator = new Paginator($events, $count, $numPerPage, $page, ['path' => $request->url(), 'query' => $request->query()]);
return $paginator;
});
If you want to add groupBy to your data the you should use LengthAwarePaginator object as updated in laravel 5
Try this,
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
$page = ($request->input('page') != null) ? $request->input('page') : 1;
$perPage = 1;
$sliced = array_slice($data, 0, 5); //you can these values as per your requirement
$paginator = new Paginator($sliced, count($data), $perPage, $page,['path' => url()->current(),'query' => $request->query()]);
return $paginator;
$data is your data object and fifth parameters are for next and prev urls
Refer this for more information about paginator,
https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Builder.html#method_paginate
As stated in the Laravel docs...Note: Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually. Docs