Laravel sum/count nested relation - laravel

i have a problem with simple relations.
Tables looks like that:
table players
id user_id game_id
1 2 1
2 5 1
3 3 1
4 4 2
5 2 2
table games
id result (win or lose)
1 1
2 0
What i need in result is:
Players Wins Losses
John 3 2
Philip 2 2
Jack 1 3
I tried alot of queries but i cant get proper result.
This one
`"select * from `players` inner join `games` on `players`.`game_id` = `games`.`id`"`
This one is best i can do, but its raw and no idea how to rewrite it to DB:: or Eloquent. And its not grouping anyway.

you can directly count from relation with model.
$category = Category::find($id);
$category->children->count();

Related

How to create a lot of categories for card laravel voyager

image
image2
Now I have like this
image3
but I want have more categories
like this
How to select many category?
Let’s say in your case, we need to define the category for every features. So feature can have many categories and inverse category can have many features. Thus, it will be a Many To Many relationships.
To accomplish this we need to create 3 tables features, categories, and intermediate table feature_category. The feature_category table will have the feature_id and category_id column which connects both feature and category table and this intermediate table called the pivot table.
Here are the table structures:
features
id - integer
name - string
categories
id - integer
name - string
feature_category
feature_id - integer
category_id - integer
=> category
id name
-- -------
1 Category 1
2 Category 2
3 Category 3
4 Category 4
=> features
id name
-- -------
1 Feature 1
2 Feature 2
3 Feature 3
4 Feature 4
=> feature_category
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
3 3 2
4 3 3
4 3 4
===============================
Our feature_category table before sync() operation:
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
3 2 3
4 2 4
5 3 2
6 4 3
Laravel Sync() example:
<?php
use App\Models\Feature;
$user = Feature::find(2);
// Want to keep only "Category 2" (Id 2) category
$user->category()->sync([2]);
After performing the above operation, our feature_category table will look like below:
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
5 3 2
6 4 3
Checkboxes or dropdowns can be used from the frontend to select multiple categories for features and sync() method can be used to update feature cards accordingly.
First you need to create new table feature_category with two fields :
(same type of features.id) feature_id
(same type of categories.id) category_id
Second create belongsToMany relationship directly in Voyager.
Example :

Distinct on two columns with same data type

In my game application I have a combats table:
id player_one_id player_two_id
---- --------------- ---------------
1 1 2
2 1 3
3 3 4
4 4 1
Now I need to know hoy many unique users played the game. How can I apply distinct, count on both columns player_one_id and player_two_id?
Many thanks.
By using union you can get unique distinct value.
$playerone = DB::table("combats")
->select("combats.player_one_id");
$playertwo = DB::table("combats")
->select("combats.player_two_id")
->union($playerone)
->count();

How to get product list related to other sale transaction count in Laravel?

I'm a newbie in Laravel and working on a small e-commerce project. I need to get products list that related to transaction count in the transaction table. Below is my demo data.
tb_product:
product_id product_name
1 A
2 B
3 C
tb_transaction:
transaction_id product_id
1 2
2 1
3 2
4 2
5 1
6 3
From tb_transaction. I have sale list as below
B = 3sales, A =2sales, C=1sale
I need to generate Laravel eloquent result like [{B...},{A...},{C...}] (count from max to min). I had tried some from google but no luck. Appreciated for advice, Thanks.
Try the below query,
DB::table('tb_product')
->select('tb_product.product_id', 'tb_product.product_name', DB::raw("count('tb_transaction.product_id') as salesCount"))
->join('tb_transaction', 'tb_transaction.product_id', 'tb_product.product_id')
->orderBy('salesCount', 'DESC')
->groupBy('tb_product.product_id', 'tb_product.product_name')
->get()

Construct Optimized Eloquent Query

I have this table:
Permits Table
id parent_id user_id renewed_at
1 0 1 2019-01-01
2 0 8 2019-01-01
3 1 7 2019-01-02
4 1 2 2019-01-03
5 2 3 2019-01-02
6 0 2 2019-01-02
7 6 9 2019-01-03
8 2 2 2019-01-09
9 0 3 2019-01-10
10 9 2 2019-01-11
11 9 5 2019-01-12
The permits undergoes on renewal indicated on the parent_id column.
The thing is the permit can be renewed by different persons indicated on user_id field. A user can be considered as the owner of the permit if he is the latest one who made the renewal.
How to construct an optimized eloquent query that will get all permit that was owned by a specific person, for example = user with id number 2
What I did is:
Get all latest record
Filter the result from step 1 if user_id=2
The problem here is that this table has 100K rows making my query very expensive.
UPDATE 1:
$records_raw = Permits::groupBy('parent_id')
->where('parent_id', '>', 0)
->get([DB::raw('MAX(id) as id')])
->pluck('id');
$my_permits = Permits::where('user_id', Auth::id())
->whereIn('id', $records_raw)
->get();
You can use laravel Eloquent
for example you want the last record of user 2 (user_id=2)
$user_id = 2;
Permits::where('user_id',$user_id)->orderBy('renewed_at','desc')->first();
it takes the latest record added for user_id=2.....means record 8
8 2 2 2019-01-09
this is optimize query and not expensive
Update:
$user_id = 2;
Permits::where('user_id',$user_id)->where('parent_id','!=',0)->orderBy('renewed_at','desc')->get();
This will return what you want
hope this is helpfull
Do not use a query cache and recommend to profile your queries Using
DebbugBar ->https://github.com/barryvdh/laravel-debugbar
Thanks and i hope to be a helpful comment

Select and sum multiple columns for statistic purposes with Laravel query

I have one table scores where I have saving users scores. It's looks like this
table `scores`
id | points | user_id
1 5 1
2 2 1
3 4 1
4 1 3
5 10 2
I want to select each user, sum his points and show as a ranking. The result from above should be
user_id | points
1 11
2 10
3 1
The query with which I came up is
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->first();
The problem is in ->first() because it's return only one result.. it is working as must. If I try to use ->get() instead I've got Undefined property error. How should I use this?
The query which is working in phpmyadmin
SELECT count(id) as numberId, sum(points) as numberOfPoints FROM `points` GROUP BY `user_id`
You can use something like this
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->get();
foreach($sumPoints as $point){
dd($point); //OR dd($point->numberOfPoints)
}

Resources