Laravel leftJoin returns null from 2nd table - laravel

I have 2 table duty_sheets
centerId | centerName | p1 | p2 | p3 | p4 | ...p22 | examiId
1 | xyz | 1 | 5 | 8 | 7 | 1 | 1
2 | abc | 9 | 1 | 6 | 6 | 1 | 1
and feedback
id | centerId | inspectorId | A | B | C | examiId
1 | 1 | 1 | 1 | 5 | 8 | 1
2 | 2 | 9 | 9 | 1 | 6 | 1
here is my code
$center = DutySheet::select('duty_sheets.centerId', 'duty_sheets.centerName','feedback.id')
->leftJoin('feedback', function ($leftJoin) {
$leftJoin->on('duty_sheets.examId', 'feedback.examId')
->where("duty_sheets.centerId", 'feedback.centerId')
->where("feedback.inspectorId", 1);
})
->where("duty_sheets.examId", 1)
->where("p20", 1)
->get();
dd($center);
to retrieve "All rows from DutySheet where p20 = 1 and dutysheet.examId = 1, and relevant rows from feedback depend on centerId, inspectorId and examId.
The problem is that the query return feedback.id as null while the record exist in feedback table with the ids.
Laravel version = 9

The problem is in left Join
->where("duty_sheets.centerId", 'feedback.centerId')
This build a where against the value 'feedback.centerId'
duty_sheets.centerId='feedback.centerId'
You need use
->on("duty_sheets.centerId",'=', 'feedback.centerId')
Or
->whereColumn("duty_sheets.centerId", 'feedback.centerId')

Related

how to get many to many relationship data in laravel?

Suppliers table
id | name
1 | supplier1
2 | supplier2
Products table
id | name
1 | product1
2 | product2
Attributes table
id | name
1 | width
2 | heigh
3 | weight
4 | volume
supplier_product table
supplier_id | product_id | price
1 | 1 | 1000
1 | 2 | 1500
2 | 1 | 1100
attribute_product
supplier_id | product_id | attribute_id | value
1 | 1 | 1 | 10
1 | 1 | 2 | 15
1 | 1 | 3 | 20
1 | 2 | 1 | 11
1 | 2 | 2 | 16
2 | 1 | 1 | 10
2 | 1 | 2 | 13
How to get attribute for each product for each supplier?
You can use the hasManyThrough relationship (https://laravel.com/docs/7.x/eloquent-relationships#has-many-through) as long as you modify the structure of your tables.
Supplier Model
public function attributes()
{
return $this->hasManyThrough('App\Attribute', 'App\Product');
}

LINQ Code that counts employee gender in each position and group by department and place in a matrix table

I just want to ask on how to create an LINQ code that can fill up my html table.
Please look at my Tables below
Table EMP: note* my "Male" is boolean
+----+---------------+--------+--------+
| id | Male| JS_REF |DEPT_ID | POS_ID |
+----+---------------+--------+--------+
| 1 | 1 | 1 | 2 | 3 |
| 2 | 0 | 2 | 2 | 3 |
| 3 | 1 | 3 | 1 | 2 |
| 4 | 1 | 2 | 4 | 2 |
| 5 | 1 | 1 | 5 | 5 |
| 6 | 0 | 4 | 6 | 1 |
| 7 | 1 | 1 | 1 | 1 |
| 8 | 0 | 2 | 2 | 3 |
+----+---------------+--------+--------+
Table:JOB_STATUS
+----+--------------------+
| id | JS_REF| JS_TITLE |
+----+--------------------+
| 1 | 1 |Undefined |
| 2 | 2 |Regular |
| 3 | 3 |Contructual |
| 4 | 4 |Probationary|
+----+--------------------+
Table:DEPTS
+----+--------------------+
| id | DEPT_ID| DEPT_NAME |
+----+--------------------+
| 1 | 1 |Admin |
| 2 | 2 |Accounting |
| 3 | 3 |Eginnering |
| 4 | 4 |HR |
+----+--------------------+
Table: POSITIONS
+----+--------------------+
| id | POS_ID| DEPT_NAME |
+----+--------------------+
| 1 | 1 |Clerk |
| 2 | 2 |Accountant |
| 3 | 3 |Bookeeper |
| 4 | 4 |Assistant |
| 5 | 5 |Mechanic |
| 6 | 6 |Staff |
+----+--------------------+
I'd made a static table on what will be the outcome of the LINQ code
Here's the picture:
Here's what i've tried so far:
SELECT tb.DEPT_NAME,TB.JS_TITLE, TB.Male, TB.Female, (TB.Male + TB.Female) AS 'Total Employees' FROM
(
SELECT JS_TITLE,DEPT_NAME,
SUM(CASE WHEN MALE = 1 THEN 1 ELSE 0 END) AS Male,
SUM(CASE WHEN MALE = 0 THEN 1 ELSE 0 END) AS Female
FROM EMP
left join JOB_STATUS on JOB_STATUS.JS_REF = EMP.JS_REF
left join DEPTS on DEPTS.DEPT_ID = EMP.DEPT_ID
GROUP BY JS_TITLE,DEPT_NAME
) AS TB
ORDER BY CASE WHEN TB.MALE IS NULL THEN 1 ELSE 0 END
If anyone can help me or give me some tips on how can I implement this im stuck in this part.
101 is total count for male, 23 for female. (the values are just copy and pasted, that's why the values are the same)
(Actual data result)

Laravel Select unique count with groupBy

I am trying to get the count of unique batches in gift_code table for each campaign. The gift_code table is joined to campaign table by campaign_id.
Here is some sample data for campaign table.
--------------+--------------
|campaign_id | name |
--------------+--------------
| 1 | abc |
--------------+--------------
| 2 | xyz |
--------------+--------------
Below is some sample data for gift_code table.
--------------+------------------------+--------------+
|gift_code_id | campaign_id | batch | unique_code |
--------------+-------------+----------+---------------
| 1 | 1 | 1 | zxc23 |
--------------+-------------+----------+--------------+
| 2 | 1 | 2 | rtc26 |
--------------+-------------+----------++-------------+
| 3 | 2 | 1 | z8723 |
--------------+-------------+----------+--------------+
| 4 | 2 | 2 | h7c26 |
--------------+-------------+----------++-------------+
| 5 | 2 | 2 | rrcf6 |
--------------+-------------+----------++-------------+
| 6 | 2 | 3 | r7y28 |
--------------+-------------+----------++-------------+
| 7 | 2 | 3 | bnc26 |
--------------+-------------+----------++-------------+
$campaign = DB::table('campaign')
->select('campaign.*', DB::raw('count(gift_code.batch) as batch_count')->groupBy('gift_code.campaign_id')->groupBy('gift_code.batch'))
->leftjoin('gift_code', 'campaign.campaign_id', '=', 'gift_code.campaign_id')
->get();
My expected results are:
--------------+-------------------------+
|campaign_id | name |batch_count|
--------------+-------------+-----------+
| 1 | abc | 2 |
--------------+-------------+-----------+
| 2 | xyz | 3 |
--------------+-------------+-----------+
Try below query
$data = \DB::table('campaign as c')
->leftJoin('gift_code as gc','c.campaign_id','=','gc.campaign_id')
->select('c.*',\DB::raw('COUNT(distinct(gc.batch)) as batch_count'))
->groupBy('c.campaign_id')
->get();

How to sort data by hierarchy

Lets say I have some data like this
Name | ID | ParentID | Level
------------+-----+----------+-------
Fruits | 1 | 0 | 1
Vegetables | 2 | 0 | 1
Apple | 3 | 1 | 2
Banana!! | 4 | 1 | 2
Tomato | 5 | 2 | 2
Potato | 6 | 2 | 2
red | 7 | 5 | 3
green | 8 | 5 | 3
How to sort (compare) this data to get a result like this:
Name | ID | ParentID | Level
------------+-----+----------+---------
Fruits | 1 | 0 | 1
Apple | 3 | 1 | 2
Banana!! | 4 | 1 | 2
Vegetables | 2 | 0 | 1
Tomato | 5 | 2 | 2
red | 7 | 5 | 3
green | 8 | 5 | 3
Potato | 6 | 2 | 2
Background is that I have a model-collection with models and I want to add them according to the hierarchy given by ID/ParentID

How to build pivot table through linq using c#

I have got this datatable in my c# code:
Date | Employee | Job1 | Job2 | Job3 |
---------|----------|------|------|-------|
1/1/2012 | A | 1.00 | 1 | 1 |
1/1/2012 | B | 2.5 | 2 | 2 |
1/1/2012 | C | 2.89 | 1 | 4 |
1/1/2012 | D | 4.11 | 2 | 1 |
1/2/2012 | A | 3 | 2 | 5 |
1/2/2012 | B | 2 | 2 | 2 |
1/2/2012 | C | 3 | 3 | 3 |
1/2/2012 | D | 1 | 1 | 1 |
1/3/2012 | A | 5 | 5 | 5 |
1/3/2012 | B | 2 | 2 | 6 |
1/3/2012 | C | 1 | 1 | 1 |
1/3/2012 | D | 2 | 3 | 4 |
2/1/2012 | A | 2 | 2 | 2 |
2/1/2012 | B | 5 | 5 | 2 |
2/1/2012 | D | 2 | 2 | 2 |
2/2/2012 | A | 3 | 3 | 3 |
2/2/2012 | B | 2 | 3 | 3 |
3/1/2012 | A | 4 | 4 | 2 |
Now I want to create another DataTable which would look like this:
Job1
Employee | 1/1/2012 | 1/2/2012 | 1/3/2012 | 2/1/2012 | 2/2/2012 |
---------|----------|----------|----------|----------|----------|
A | 1.00 | 3 | 5 | 2 | 3 |
B | 2.50 | 2 | 2 | 5 | 2 |
C | 2.89 | 3 | 1 | - | |
D | 4.11 | 1 | 2 | 2 | |
Total | 10.50 | 9 | 10 | 9 | 5 |
Please suggest how to make this pivot table using Linq and C#.
var query = from foo in db.Foos
group foo by foo.Date into g
select new {
Date = g.Key,
A = g.Where(x => x.Employee == "A").Sum(x => x.Job1),
B = g.Where(x => x.Employee == "B").Sum(x => x.Job1),
C = g.Where(x => x.Employee == "C").Sum(x => x.Job1),
D = g.Where(x => x.Employee == "D").Sum(x => x.Job1),
Total = g.Sum(x => x.Job1)
};
You can also apply OrderBy(x => x.Date) to query.
You won't be able to do this with LINQ due to the dynamic nature of the columns. LINQ to SQL needs a static way to map result set fields to property values. Instead, you can look into the PIVOT SQL statement and fill the results into a DataTable
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

Resources