Remove unwanted array from json - php

I'm new to Laravel. I'm creating a json structure using a function. This is my current output:
{
"success": "1",
"data": [
{
"category_type_id": 1,
"category_type": "Study",
"category_icon": "http://192.168.1.132:8000/images/category/study.svg"
},
{
"category_type_id": 2,
"category_type": "Sports",
"category_icon": "http://192.168.1.132:8000/images/category/game.svg"
},
{
"category_type_id": 3,
"category_type": "Other",
"category_icon": "http://192.168.1.132:8000/images/category/other.svg"
}
]
}
This is my controller code:
$get_all_category = CategoryType::all();
return response()->json(['success' => '1', 'data' => $get_all_category]);
I want result without array that is started from data plz need solution

Just remove the other attributes from your from your json response:
$get_all_category = CategoryType::all();
return response()->json($get_all_category);
That will return your json like this:
[
{
"category_type_id": 1,
"category_type": "Study",
"category_icon": "http://192.168.1.132:8000/images/category/study.svg"
},
{
"category_type_id": 2,
"category_type": "Sports",
"category_icon": "http://192.168.1.132:8000/images/category/game.svg" },
{
"category_type_id": 3
"category_type": "Other",
"category_icon": "http://192.168.1.132:8000/images/category/other.svg"
}
]
If you want to keep the success atribute you can do something like this, but it would just change the old data attribute you want to get rid of to '0':
$get_all_category = CategoryType::all();
return response()->json(['success' =>'1', $get_all_category]);

Related

How to prevent duplication of object inside by pushing it to the array in php?

I have question, is it possible not to duplicate the array object by looping on it? Right now I used laravel as my backend
I have here my response which is the exchange object duplicate itself.
[
{
"exchange": {
"id": 1,
"branch": "BB1",
"old_check_no": "0001",
"cash": "250000",
"bank_deposit": "1000000",
"offset": "250000",
"amount": "10000",
"over_under": null,
"checkDate": "2021-09-11",
"remarks": "1",
"date_closed": "2021-09-11"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "001",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
}
},
{
"exchange": {
"id": 1,
"branch": "BB1",
"old_check_no": "0001",
"cash": "250000",
"bank_deposit": "1000000",
"offset": "250000",
"amount": "10000",
"over_under": null,
"checkDate": "2021-09-11",
"remarks": "1",
"date_closed": "2021-09-11"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "002",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
}
},
]
Now my goal is to push the exchange without duplication:
[
{
"exchange": {
"id": 1,
"branch": "BB1",
"old_check_no": "0001",
"cash": "250000",
"bank_deposit": "1000000",
"offset": "250000",
"amount": "10000",
"over_under": null,
"checkDate": "2021-09-11",
"remarks": "1",
"date_closed": "2021-09-11"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "001",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
},
"exchange_list": {
"exchange_id": 1,
"new_check_no": "002",
"new_check_bank": "bank",
"new_check_branch": "Lagros"
}
}
]
Here is what my foreach loop like and how i push the array object.
$myArray = [];
foreach($exchange_check as $primary_array) {
foreach($exchange_lists as $second_array) {
if($second_array->exchange_id == $primary_array->id) {
array_push($myArray, (object)[
'exchange' => $primary_array,
'exchange_list' => $second_array,
]);
}
}
}
Thanks
You should add exchange data to array only once in first loop:
$myArray = [];
foreach($exchange_check as $primary_array) {
$idx = array_push($myArray, ['exchange' => $primary_array]);
foreach($exchange_lists as $second_array) {
if($second_array->exchange_id == $primary_array->id) {
//array_push() returns the new number of elements in the array,
//to get currently added array element we should subtract 1 from this number
$myArray[$idx-1]['exchange_list'][] = $second_array;
}
}
}
And in second loop add only exchange_list data to array.

How To Get Specifie Product in Laravel Array

im trying to get those products that have css and html technology.note that (html and css) are not the only parameters that can be used.sometimes could be more, for example ['java','html,'python',...] and so on
in this result i have 2 product but it should be 1,the one with (id:1) becuz only this product have both (html and css)technology
[
{
"id": 1,
"title": "First Product",
"prte": [
{
"product_id": 1,
"technology_id": 1,
"technology": {
"id": 1,
"title_en": "HTML"
}
},
{
"product_id": 1,
"technology_id": 2,
"technology": {
"id": 2,
"title_en": "CSS"
}
}
]
},
{
"id": 2,
"title": "Second Product",
"prte": [
{
"product_id": 2,
"technology_id": 1,
"technology": {
"id": 1,
"title_en": "HTML"
}
}
]
}
]
and this one is the Controller
$products = Product::with(['prte' => function ($query){
$query->with(['technology' => function ($query){
$query->whereIn('title_en',['HTML','CSS']);
}]);
}])->get();
return view('find',compact('products'));
hope u guys could help me
Try where instead of whereIn.
$products = Product::with(['prte' => function ($query){
$query->with(['technology' => function ($query){
$query->where('title_en','HTML')->where('title_en','CSS');
}]);
}])->get();
return view('find',compact('products'));

laravel change object when use groupBy in collection

when i use groupBy with laravel
i use it for api but response show like this example
$data = Order::where('id_user',Auth::id())->with('resturant')->orderBy('created_at', 'desc')->get()->groupBy('uuid');
return response()->json([
'data'=>$dataa,
'state'=>true,
]);
response showing
"data": {
"11447": [
{
"id": 75704,
"uuid": "11447",
...........
"resturant": {
"id": 5,
"category_id": 1,
...........
}
}
],
"49262": [
{
"id": 75702,
"uuid": "49262",
...........
"resturant": {
"id": 5,
"category_id": 1,
...........
}
},
]
},
"state": true
}
i need remove number in object
"11447": [
how ?!
please use this
$data = Order::select(DB::raw('id,uuid,count(*) as count_order'))->groupBy('uuid')->get();
return response()->json([
'data'=>$data,
'state'=>true,
]);

how to get proper nested json format?

I want to display the result of join query in nested JSON format but it will not work I am getting wrong input. I used two tables as
category_type={category_type_id,category_type_name,category_icon};
main_category={main_category_id,category_type_id,category_name}
I want display nested JSON result but not getting plz need solution.
Controller:
$data= DB::table('table_main_category')
->join('table_category_type','table_main_category.category_type_id','=','table_category_type.category_type_id')
->select('table_category_type.*','table_main_category.*')
->get();
return Response::json(array(
'success' => '1',
'data' => $data),
200
);
json output:
{
"success": "1",
"data": [
{
"category_type_id": 2,
"category_type": "Sports",
"category_icon": "http://192.168.1.132:8000/images/category/game.svg",
"main_category_id": 1,
"category_name": "Popular Sports"
},
{
"category_type_id": 2,
"category_type": "Sports",
"category_icon": "http://192.168.1.132:8000/images/category/game.svg",
"main_category_id": 2,
"category_name": "Team Sports"
}
]
}
required json:
"success": "1",
"data": [{
"category_type_id": 2,
"category_type": "Sports",
"category_icon": "http://192.168.1.132:8000/images/category/game.svg",
"main_category": {
"main_category_id": 1,
"category_name": "Popular Sports"
}
},
{
"category_type_id": 2,
"category_type": "Sports",
"category_icon": "http://192.168.1.132:8000/images/category/game.svg",
"main_category": {
"main_category_id": 2,
"category_name": "Team Sports"
}
}
]
A workaround could be to update the data before you send the Response:
foreach ($data->toArray() as $elem) {
$elem['main_category'] = [];
$elem['main_category']['main_category_id'] = $elem['main_category_id'];
unset($elem['main_category_id']);
// ... and so on
}
use this code to restructure your data :
$data= DB::table('table_main_category')
->join('table_category_type','table_main_category.category_type_id','=','table_category_type.category_type_id')
->select('table_category_type.*','table_main_category.*')
->get();
return Response::json(array(
'success' => '1',
'data' => $data.map( i => {
const {main_category_id ,category_name , ...j } = i ;
return ({...j , main_category: {
main_category_id,
category_name }
});
}) ,
200
);
You can use Eloquent relationship. First create your Models.
use Illuminate\Database\Eloquent\Model;
class MainCategory extends Model
{
public $table = 'table_main_category';
}
class CategoryType extends Model
{
public $table = 'table_category_type';
public function mainCategory()
{
return $this->belongsTo('App\MainCategory', 'category_type_id', 'category_type_id');
}
}
Then in your controller you can query it like
$data = CategoryType::with('mainCategory')->get();
This will load category types with their corresponding main categories

Return selected keys in collection for json

I have this data in method of controller as you see I deleted some keys in method:
$foods = Food::get()->map(function($value){
return collect($value->toArray())->except('pivot', 'deleted_at', 'created_at', 'updated_at');
});
return HttpHelpers::sendJsonData($foods, 200);
and the api response returns this:
{
"success": true,
"data": [
{
"id": 1,
"title": "food1",
"default_price": 2353465456,
"main_meal": 1,
"labels": [
{
"id": 1,
"title": "type1",
"type": "food",
"created_at": "2018-08-23 03:55:33",
"updated_at": "2018-08-23 03:55:33",
"pivot": {
"labelable_id": 1,
"label_id": 1,
"labelable_type": "App\\Models\\Panel\\Food"
}
}
]
},
{
"id": 2,
"title": "food2",
"default_price": 1000,
"main_meal": 0,
"labels": [
{
"id": 1,
"title": "type2",
"type": "food",
"created_at": "2018-08-23 03:55:33",
"updated_at": "2018-08-23 03:55:33",
"pivot": {
"labelable_id": 2,
"label_id": 1,
"labelable_type": "App\\Models\\Panel\\Food"
}
}
]
}
]
}
now my problem is that I do not want to return some keys, like labalable_id and labelable_type in pivot and created_at in labels, please offer me the best way
You can use the makeHidden method for hiding data to JSON. Haven't tried this code but, i think it should work.
https://laravel.com/docs/5.6/eloquent-serialization#hiding-attributes-from-json
$foods = Food::get()->map(function($value){
foreach($value->labels as $label){
$label = $label->makeHidden(['created_at']);
$label->pivot = $label->pivot->makeHidden(['labelable_id', 'labelable_type']);
}
return collect($value->toArray())->except('pivot', 'deleted_at', 'created_at', 'updated_at');
});
If the data response is coming through the stored procedure then in that case you need to store the Pivot table data in a temporary table and select the required keys that you require and display it through API in json.

Categories