This is my query to fetch data from 2 different table.
$variant = Variant::with(['v_detail' => function($q){
$q->select('variant_dtl_name');
}])->where('product_id','=',$productId)->get();
There is output, but v_detail returning empty list
result:
created_at: "2015-11-07 12:37:26"
id: 1
product_id: 30
updated_at: "2015-11-07 12:37:26"
v_detail: []
variant_name: "Pricing"
But with these query:
$variant = Variant::with('v_detail')->where('product_id','=',$productId)->get();
The result is:
created_at: "2015-11-07 12:37:26"
id: 1
product_id: 30
updated_at: "2015-11-07 12:37:26"
v_detail: [{id: 1, variant_id: 1, variant_dtl_name: "Adult", variant_dtl_price: 25,…},…]
0: {id: 1, variant_id: 1, variant_dtl_name: "Adult", variant_dtl_price: 25,…}
1: {id: 2, variant_id: 1, variant_dtl_name: "Senior", variant_dtl_price: 15,…}
2: {id: 3, variant_id: 1, variant_dtl_name: "Children", variant_dtl_price: 8,…}
variant_name: "Pricing"
Now, on the query that work, how can I fetch a specific column names. Thanks!!
You have this:
$variant = Variant::with(['v_detail' => function($q)
{
// Either add the related foreign key or select all
$q->select('related_foreign_key', 'variant_dtl_name');
}])->where('product_id','=',$productId)->get();
Since you are selecting only a single field which is variant_dtl_name then it's not possible to find out the related models because the relation builder foreign key is required. So, you have to select that foreign key as well. Notice the related_foreign_key in sub-query so use the right one, probably that is variant_id but not sure because you didn't mention anything about that.
Related
I have been stuck on this for several hours now. I need to write a query that returns all documents where (Field A - Field B) > N
// sample data
{ _id: '...', estimated_hours: 0, actual_hours: 0 },
{ _id: '...', estimated_hours: 10, actual_hours: 9 },
{ _id: '...', estimated_hours: 20, actual_hours: 30 }
Borrowing answers from this stack question I wrote the below, In my mind this should have worked, however I am consistently getting records back that do not match the query...
## Attempt 1
n = 0
records = API::Record.where('$where': "(this.estimated_hours - this.actual_hours) > #{n}")
## should return the following, but im getting additional records
#=> [{ _id: '...', estimated_hours: 10, actual_hours: 9 }]
I know I can likely accomplish this with $project however i have to explicitly tell $project what fields I want returned. I need all the fields to be returned, we use a third party library that handles pagination
play
db.collection.find({
$where: "(this.estimated_hours - this.actual_hours) > 1"
})
Similar example for reference
I have table where I want to find difference in certain columns. I'm doing it with diff():
$inventoryitems1 = InventoryItem::where('inventory_id', $request->get('inventory1'))
->select('owner_id', 'location_id', 'asset_id')
->get();
$inventoryitems2 = InventoryItem::where('inventory_id', $request->get('inventory2'))
->select('owner_id', 'location_id', 'asset_id')
->get();
$difference = $inventoryitems2->diff($inventoryitems1);
And then i get empty array. But if I don't have select in my collection. It work, it shows all different rows. Is there anyother way to do this or this one is right but I'm not doing it the right way?
UPDATE
I have $inventoryitems1 collection that looks like this:
0: {asset_id: 2, owner_id: 2, location_id: 1}
1: {asset_id: 3, owner_id: 2, location_id: 1}
and $inventoryitems1 collection that looks like this:
0: {asset_id: 2, owner_id: 2, location_id: 1}
1: {asset_id: 3, owner_id: 6, location_id: 1}
I'm trying to find differences in those two collections, where we can see that owner_id is different in second collection. How can I only get those objects where there was some change (difference).
I got a response as follows. I need to the print the "count(*)" field along with some other data like "Duke (2)" where 2 is the count.
How can I print the value in the laravel template.
{
parts_model_id: 8,
parts_id: 29,
parts_title: "duke multiple model",
model_id: 1,
model_name: "OPAH2",
created_at: "2017-07-18 17:02:10",
updated_at: "2017-07-18 17:02:10",
count(*): 2
}
The code
$models = PartsModel::with('model')->selectRaw('*, count(*)')->groupBy('model_id')->get();
Thank you
You can do it like this :
$models = PartsModel::with('model')
->selectRaw('*, count(*) AS countElements')
->groupBy('model_id')
->get();
And you can access the count using (after looping :)) :
$model->countElements;
I have 2 columns in my awards table: ranking (integer) and name (string). I want to create a scope that shows all the records with the ranking column filled in in ascending order (1, 2, 3...), and then the rest of the records that don't have a ranking to show by name ascending (a, b, c) so it would look like this:
ranking: 1, name: "zz"
ranking: 2, name: "aaa"
ranking: nil, name: "bbbb"
ranking: nil, name: "ccc"
ranking: nil, name: "ddd"
etc...
This doesn't seem to work: scope :book_form_sort_order, -> { order("ranking ASC, name ASC").group(:ranking) }
scope :book_form_sort_order, -> { order("ranking IS NULL, ranking ASC, name ASC") }
I'm working with jqGrid and have hit an issue that I can't quite identify...
I've got a grid setup with 2 level multi-grouping, and I've also got a select list to dynamically change the grouping. This all works well so far, and I can change the grouping appropriately based on the selected option. For example:
Project 1
File 1
field 1, field 2, field 3
field 1, field 2, field 3
field 1, field 2, field 3
File 2
field 1, field 2, field 3
field 1, field 2, field 3
field 1, field 2, field 3
File 3
field 1, field 2, field 3
field 1, field 2, field 3
field 1, field 2, field 3
Project 2
File 8
field 1, field 2, field 3
File 9
field 1, field 2, field 3
In this illustration, the project number is the output group and the file number is the inner group. Under each file group is a list of all of the rows in that group.
The issue is that I need an option to remove the inner grouping only and keep the outer grouping intact. I've tried the below code, but it doesn't appear to be doing anything:
if (val == "clear")
{
failingRulesGrid.jqGrid('setGridParam',
{
groupingView:
{
groupField: ['ename'],
groupOrder: ['asc']
}
}).trigger('reloadGrid');
}
This is the code for dynamically changing the multi-grouping, and it is working perfectly:
if (val != "clear")
{
failingRulesGrid.jqGrid('setGridParam',
{
groupingView:
{
groupField: ['ename', val],
groupColumnShow: [false, false],
groupOrder: ['asc', 'asc']
}
}).trigger('reloadGrid');
}
Does anyone have any thoughts on how I can remove the inner group but retain the output group? As a side note, the outer group will only ever be based on the 'ename' column so I don't need to worry about changing how the outer group is constructed.
Not sure why I didn't think of this before, but I've got a working solution. Using 'groupingGroupBy' solves this pretty easily:
if (val == "clear")
{
gridSelector.jqGrid('groupingGroupBy', 'ename',
{
groupColumnShow: [false],
groupCollapse: true
});
}