Laravel groupby to get sum but only based on another columns value - laravel

I have multiple joins that I am doing to get one list. I can group by a certain value to get a sum but need to merge another column that is not being grouped by. My tables look kinda like this (simplified)
Quote Table
id|Name |Location |
---------------------------------
1|My First Quote|123 Main Street|
Quote Items table
quote_id| item_id
--------------------
1 | 1
--------------------
1 | 2
Items Table
id|Name |Quantity|Type
--------------------------------
1|Dog Food|2 |product
2|Delivery|1 |service
I need to show a final result like this in datatables
Name Location Qty Item
My First Quote | 123 Main Street | 2 | Dog Food
So basically i need to sum on item.quantity and show the product type as item. I have the group by working with a raw sum on the item grouped by item.id where item type is product but the where gets rid of the item service that i need to show on the row.

You can try this:
DB::table("Quote")
->select('Quote.Name','Quote.Location')
->(['Items.Quantity AS Qty','Items.Name AS Item'])
->join("Items","Items.item_id","=","Quote.quote_id")
->get();

You can define a relationship for quotes table
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Quote extends Model
{
public function items()
{
return $this->belongsToMany(Item::class);
}
}
after that, call a sum method with that relation for the column you want.
Quote::withSum('items','Quantity')->get();

Related

Laravel, how to search for product features? get the ids where every item in array is present in table

I have a table with product features
table structure below
..|product_id | feature_id|created_at
..|1 | 5 |time_stamp
..|1 | 6 |time_stamp
..|2 | 6 |time_stamp
..|2 | 9 |time_stamp
.
.
.
how to write query to search with array [5,6] and get result of product id 1
You can try like
product_feature_data is a model name
Proudct::whereHas('product_feature_data',function($q) {
$q->whereIn('feature_id', [5,6]);
})->get();

How to find total count of distinct items along with item using Room?

Let's say I have a table kids_data
id
kid_name
father_name
1
Alisa
Bosconovitch
2
Ebby
Bosconovitch
3
John
Peter
4
simmy
Alladin
5
sara
Alladin
Now I want to fetch distinct names and their count too.
So if I perform
SELECT DISTINCT father_name from kids_data
What I will get is
father_name
Bosconovitch
Peter
Alladin
I want count of each kid too.
As Bosconovitch & Alladin have Two children then I want that number also.
Using Room Database in Android? How?
My class is
data class FatherData(
val kid_count: Int,
val father_name: String
)
In Dao
// what Query or how can i do this to achieve that, please.
#Query("SELECT DISTINCT father_name from kids_data")
fun getFatherData(): List<FatherData>
This does what I wanted!
This query will be used.
#Query("select father_name, count(father_name) as kid_count from kids_data group by father_name")
fun getFatherData(): List<FatherData>

Laravel : How to show a multiple rows from DB in one row in view blade

In my Laravel project, I have a multiple rows in my DB table, similar to each other in every thing except ID, other column called stage
Stages table
ID
name
1
first
2
second
3
third
subjects Table
ID
name
stage_id
1
English
1
2
English
2
3
English
3
I need to show em in a blade like this
#
name
stage
1
English
first, two, three, etc
I already have the relations in my modals, and I am already showing them in my blade by loop, but I just want to group the rows by name and show the different stages
I hope that I explained the case well, I am not very good at explaining
Updates : before editing I wrote a dummy data now I hope I explained the issue in more details
The project idea is that you can create more than faculty
and every faculty has Stages and sections and every section has specialties and every specialty has subjects
now about subjects and stages
user can create subject like English and make it available to the first stage and 2nd and third ..etc ( the available stages in this faculty)
The Stages belongs to Faculty
The Subjects belongs to Stage
so the relations are
Subject Model
public function stage()
{
return $this->belongsTo(Stage::class);
}
Stage Model
public function subjects()
{
return $this->hasMany(Subject::class);
}
As you have "English" Multiple Times, you should have a subject Table :
Stages table
ID
name
1
first
2
second
3
third
Subject table
ID
name
1
English
stage_subject Table :
ID
stage_id
subject_id
1
1
1
1
2
1
1
3
1
Subject Model
public function stage()
{
return $this->belongsToMany(Stage::class);
}
Stage Model
public function subjects()
{
return $this->belongsToMany(Subject::class);
}
Then in your foreach:
#php($subjects = Subject::with('stages')->get())
#foreach($subjects as $subject)
{{ $subject->id }} | {{$subject->name}} | {{$subject->stages->pluck('name')->implode(', ')}}
#endforeach
If you don't / can't do a pivot table, then you can use collection :
#php($subjects = Subject::with('stages')->get())
#foreach($subjects->groupBy('name') as $name => $subjects)
{{ $subjects->first()->id }} | {{$name}} | {{$subjects->pluck('stage.name')->implode(', ')}}
#endforeach

How to call multiple filters in DHTMLX Grid

I would like to call multiple functions in DHTMLX Grid.
I have a table which has the following 7 columns
EX:
---------------------------------------------------------------
No. | Name | Age | Gender | Marital Status | Education | City |
---------------------------------------------------------------
I would like this grid to be filtered by multiple filter conditions.
For Ex: Filter the grid to have only the gender 'Male' whose age is below 35.
Currently my doFilter() function looks like this.
function doFilter() {
mygrid.filterBy(3,'M',true);
mygrid.filterBy(2,function(a){ return (a > 55);} );
}
But the grid is only filtered by age not by Gender column.
Please let me know how to apply multiple filter condition in DHTMLX Grid.
You need to use it as
mygrid.filterBy(3,'M');
mygrid.filterBy(2,function(a){ return (a > 55);}, true);
second filterBy call must have true as last parameter, to preserve results of previous filterBy call.

Using filter inside a calculated memeber

I'm having some MDX issues, I want to calculate how many products I have per type per version, this would be my output.
ProductID | QtyProductAVersionA | QtyProductAVersionB | QtyProductBVersionA | QtyProductBVersionB |
I have this MDX so far
WITH MEMBER [Measures].[ProductAVersionA]
AS SUM([DimProduct].[ProductName].&[ProductA],[Measures].[ProductQty])
SELECT NON EMPTY (
[Measures].[ProductAVersionA]) ON COLUMNS,
NON EMPTY [DimOrg].[ProductID].[ProductID].MEMBERS ON ROWS
FROM [Sales]
WHERE([DimCustomers].[Customer Area].&[United States])
But this returns the total of product A, I want only the product A filtered by version A. I can't use it in the WHERE clause since not all my products have the same versions.
Is there any way I can achieve this with a Filter expression inside the calculated memeber? I tried to used but I kept getting an error.
FYI product version is in another dimension [DimVersion]
Any help would be appreciated
You could try filtering the ProductAVersionA calculated member by the DimVersion 'A' member as follows:
member [Measures].[ProductAVersionA] as
sum(([DimProduct].[ProductName].&[ProductA], [DimVersion].[VersionName].&[VersionA]), [Measures].[ProductQty])

Resources