First of all I am new to laravel.
This is my controller
$itemsList=Items::all()->where('shop_id',$request->shop_id);
return response()->json(['data'=>$itemsList]);
This is the response
{
"data": {
"10": {
"id": 11,
"shop_id": 1,
"title": "Test",
"price_nd": 12,
"price_wd": 10,
"updated_at": "2019-11-14 00:00:00",
"created_at": "2019-11-14 00:00:00"
},
"11": {
"id": 12,
"shop_id": 1,
"title": "Test",
"price_nd": 12,
"price_wd": 10,
"updated_at": "2019-11-14 00:00:00",
"created_at": "2019-11-14 00:00:00"
},
"14": {
"id": 15,
"shop_id": 1,
"title": "Test",
"price_nd": 12,
"price_wd": 10,
"updated_at": "2019-11-14 00:00:00",
"created_at": "2019-11-14 00:00:00"
}
}
}
The problem is I want to return an array list of the items
I tried many approached but I don't know what is the problem or what I am missing
Try. Use get() method .
get() and all() both get same output but
all() method not useful for use where condition.
get() method can useful to use other conditions
$itemsList=Items::where('shop_id',$request->shop_id)->get();
return response()->json(['data'=>$itemsList]);
Try this code..
$itemsList=Items::where('shop_id',$request->shop_id)->get();
dd($itemsList); // add this step in your code
return response(['data' => $itemsList]);
You this approach.
return response(['data' => $itemsList]);
Related
Get query instead of replace foreign key by name in laravel.Get record replacement of menu_id to menu_name.Menu table contains id and menu_name.
"cart_items": [
{
"id": 1,
"cart_id": 1,
"menu_id": 5,
"quantity": "3",
"amount": "150",
"created_at": "2019-09-04 09:45:28",
"updated_at": "2019-09-04 09:45:28"
},
{
"id": 2,
"cart_id": 1,
"menu_id": 4,
"quantity": "3",
"amount": "150",
"created_at": "2019-09-04 09:54:32",
"updated_at": "2019-09-04 09:54:32"
}
]
use Laravel mutators to add menu_name for object, and add menu_id to $hidden =['manu_id']; in the model class
I use with method while fetching query.Its works
$data['cart_items'] = CartItem::with('menu')->where('cart_id',$cart->id)->get();
In my CartItem model i use eloquent orm function:
public function menu()
{
return $this->hasOne('App\Menu','id','menu_id')->select(['id','menu_name']);
}
It returns output:
"cart_items": [
{
"id": 1,
"cart_id": 1,
"menu_id": 5,
"quantity": "3",
"amount": "150",
"created_at": "2019-09-04 09:45:28",
"updated_at": "2019-09-04 09:45:28",
"menu": {
"id": 5,
"menu_name": "PODI IDLI"
}
},
{
"id": 2,
"cart_id": 1,
"menu_id": 4,
"quantity": "3",
"amount": "150",
"created_at": "2019-09-04 09:54:32",
"updated_at": "2019-09-04 09:54:32",
"menu": {
"id": 4,
"menu_name": "CORN PANEER MIX VEG SALAD WITH SAUSAGE"
}
}]
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 got a some trouble in my Laravel application, in Search function, when I do search the result is
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "4232343432",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
{
"id": 3,
"user_id": 16,
"identity_number": "213918273",
"name": "Pekerja_3",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2345234234",
"image": null,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"partner_id": null,
"skill": []
}
]
}
as you can see that "Skill" with id number 3 is empty, I want all in id number 3 is empty also.
My controller is :
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])->whereHas('skill');
And I want something like this :
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2534234234",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
]
}
The point is, if "skill" variable is empty then data not show and vice versa.
Thankyou any help will appreciate, sorry for bad english
You can add a callback to your whereHas function with the same filter as the with function
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])
->whereHas('skill', function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
})
->get();
You can try this $worker = Worker:has('skill')->get();
Only worker that have at least one skill are contained in the collection
Hello i want to remove "news_list" from php json array.
{
"news_list": [
{
"id": 2,
"group_id": 1,
"news_title": "fbb",
"news_description": "gfhgfh",
"status": "Y",
"created_at": "2017-05-11 16:04:26",
"updated_at": "2017-05-11 16:04:26"
},
{
"id": 3,
"group_id": 1,
"news_title": "ewrdf",
"news_description": "dsfsdfdsfsdffffffffffffff",
"status": "Y",
"created_at": "2017-05-12 10:59:01",
"updated_at": "2017-05-12 10:59:01"
}
]
}
Desired Output :
[
{
"id": 2,
"group_id": 1,
"news_title": "fbb",
"news_description": "gfhgfh",
"status": "Y",
"created_at": "2017-05-11 16:04:26",
"updated_at": "2017-05-11 16:04:26"
},
{
"id": 3,
"group_id": 1,
"news_title": "ewrdf",
"news_description": "dsfsdfdsfsdffffffffffffff",
"status": "Y",
"created_at": "2017-05-12 10:59:01",
"updated_at": "2017-05-12 10:59:01"
}
]
is there ant in-built PHP function for same ?.if there i s any in built function then please convey me.
There is nothing like direct function to fetch any key data of json. You need to convert your json to array or object using json_decode and then can perform or fetch what you looking for.
$res = json_decode($data); // return object
print_r($res->news_list);
Or
$res = json_decode($data, true); // return array
print_r($res['news_list']);
Try this...
$oldDataArray = json_decode('{"news_list":[{"id":2,"group_id":1,"news_title":"fbb","news_description":"gfhgfh","status":"Y","created_at":"2017-05-11 16:04:26","updated_at":"2017-05-11 16:04:26"},{"id":3,"group_id":1,"news_title":"ewrdf","news_description":"dsfsdfdsfsdffffffffffffff","status":"Y","created_at":"2017-05-12 10:59:01","updated_at":"2017-05-12 10:59:01"}]}');
$newDataArray = $oldDataArray->news_list;
echo '<pre>';
print_r($newDataArray);
echo '</pre>';
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