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
Related
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'));
Controller:
$files = File::where('agent_id', $user->id)->with('posts')->get();
Model:
public function posts()
{
return $this->hasManyThrough('App\User', 'App\Post', 'id', 'id', 'post_id', 'user_id');
}
So this return a bunch of data, for example:
{
"success": [
{
"id": 2,
"post_id": 1,
"transaction_id": 4,
"agent_id": 2,
"status": 0,
"posts": [
{
"id": 1,
"name": "john",
"email": "john#gmail.com",
"phone": "489797878",
"type": "1",
"verified": 1,
"otp": null,
"created_at": "2019-11-23 10:17:31",
"updated_at": "2019-11-23 10:17:51",
"api_token": null,
"laravel_through_key": 1
}
]
}
...
}
What I want is, exclude some data, like email or verified and etc. I tried pluck('email') and also makeHidden but no success. any idea how can I do this?
How about doing some thing like below, ( haven't tested the code but should give you a clue )
files = File::where('agent_id', $user->id)
->with(['posts' => function ($q) {
$q->select('name','phone'); // specify whatever you want
}])->get(['column1','column2']);
Just use ->get() include list of those you want to get:
example:
$files = File::where('agent_id', $user->id)->with('posts')->get(['post_id', 'status']);
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]);
my database table structure
this my purchase model
class Purchase extends Model
{
public function supplierDetails(){
return $this->belongsTo('App\Supplier', 'supplier_id');
}
public function purchasedItems(){
return $this->hasMany('App\PurchasedItem', 'purchase_id');
}
}
this my controller method
public function getReport(){
$result = Purchase::with('supplierDetails', 'purchasedItems')->get();
return $result;
}
this is my json result
[
{
"id": 74,
"supplier_id": 3,
"invoice_no": "wds2",
"supplier_details": {
"id": 3,
"name": "alexy"
},
"purchased_items": [
{
"id": 114,
"purchase_id": 74,
"item_id": 2
},
{
"id": 115,
"purchase_id": 74,
"item_id": 3
}
]
}
]
i can make relationship between these Three models 'Supplier', 'Purchase', and 'PurchaseItem' but cannot make relationship with 'Item' models
i want json response like this
[
{
"id": 74,
"supplier_id": 3,
"invoice_no": "wds2",
"supplier_details": {
"id": 3,
"name": "alexy"
},
"purchased_items": [
{
"id": 114,
"purchase_id": 74,
"item_id": 2,
"item_details": {
"id": 2,
"name": "item1"
}
},
{
"id": 115,
"purchase_id": 74,
"item_id": 3,
"item_details": {
"id": 3,
"name": "item2"
}
}
]
}
]
Any idea?
This might help, i haven't tested but you should and let me know.
public function getReport(){
$purchases = Purchase::all;
$result;
foreach($purchases as $p){
$itemsPurchased = $p->purchasedItems;
foreach($itemsPurchased as $pitem){
$pitem['item_details'] = $pitem->YOUR.FUNCTION.NAME.FOR.ITEM.DETAILS.
}
$p['purchased_items'] = $itemsPurchased;
$p['supplier_details'] = $p->supplierDetails;
$result = array($p);
}
return $result;
}
i made some changes and the below code works..
public function getAll(){
$array = Purchase::with('supplierDetails', 'purchasedItems')->get();
$purchases = json_decode($array,true);
$result = array();
foreach($purchases as $p){
$itemsPurchased = $p['purchased_items'];
foreach($itemsPurchased as $pitem){
$pitem['item_details'] = PurchasedItem::with('itemDetails', 'packageDetails')->where('purchase_id', '=', $pitem['purchase_id'])->get();
}
$p['supplier_details'] = $p['supplier_details'];
$p['purchased_items'] = $pitem['item_details'];
array_push($result, array($p));
}
return $result;
//[0] added to remove []sqauare bracket
}
Imagine I have a site with a lots of catalogs which have sections and with each section, i have items.
catalogs is belongstomany with sections.
sections is belongstomany with items.
What I want for final result is to have a nest data with:
{
"catalog_id": 1,
"catalog_name": "catalog 01",
"sections": [
{
"section_id": 1,
"name": "section 01",
"items": [
{
"item_id": 1,
"item_content": {
"name": "some content1"
}
},
{
"item_id": 2,
"item_content": {
"name": "some content2"
}
},
{
"item_id": 3,
"item_content": {
"name": "some content3"
}
}
]
},
{
"section_id": 2,
"name": "section 02",
"items": [
{
"item_id": 2,
"item_content": {
"name": "some content2"
}
},
{
"item_id": 3,
"item_content": {
"name": "some content3"
}
},
{
"item_id": 4,
"item_content": {
"name": "some content4"
}
}
]
}
]
}
How can I achieve that?
Technically I can use each-loop to get whatever I want, but I hope that is a better solution in Eloquent.
Try this
Catalogs::with('sections.items')->get();
I would add to #J.Doe answer that you can specify what you want to do. for example
Catalogs::with([
'sections' => function($query){
return $query->select('column')
->where('someCondition', 1)
->orderBy('id', 'asc');
'items' => //some other stuff
];
you can also use method with() for your relation. lets say sections related to someTable. if you have relation method someTable in your section model, then:
Catalogs::with([
'sections' => function($query){
return $query->select('column')
->with('someTable') // here's your relation
->where('someCondition', 1)
->orderBy('id', 'asc');
'items' => //some other stuff
];