how to get data where clause in another table in laravel - php

I have 2 table with relation . and i want to add where clause but this where clause is in another table . my table like this :
pencairan
+----+------------+------------+------------+
| id | induk_id | qty | harga
+----+------------+------------+------------+
| 1 | 1 | 10 | 1000
+----+------------+------------+------------+
| 2 | 1 | 20 | 3000
+----+------------+------------+------------+
| 3 | 3 | 10 | 1500
+----+------------+------------+------------+
induk_pencairan
+----+------------+------------+------------+
| id | rek_id | name | address
+----+------------+------------+------------+
| 1 | 1 | somedata| somedata
+----+------------+------------+------------+
| 2 | 1 | somedata| somedata
+----+------------+------------+------------+
| 3 | 3 | somedata| somedata
+----+------------+------------+------------+
| 4 | 3 | somedata| somedata
+----+------------+------------+------------+
so i want to do like this :
$pencairan = IndukPencairan::with('turunan_belanja')->findOrFail($id);
if (!$pencairan)
abort(404);
$id = $pencairan->id;
$rek = $pencairan->rek_id;
$digunakan = Pencairan::with('induk_pencairan')
->where('induk_pencairan.rek_id' ,$rek)
->whereNotIn('induk_id ', [$id])->sum(DB::raw('harga*qty'));
but i get error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'induk_pencairan.rek_id' in 'where clause'
(SQL: select sum(harga*qty) as aggregate from `pencairan` where `induk_pencairan`.`rek_id` = 2 and `induk_id` not in (2))
can someone help this ??
#update my model on 2 table
on Pencairan models
public function induk_pencairan()
{
return $this->belongsTo(\App\Models\IndukPencairan::class ,'induk_id');
}

you just have to join them ...
$digunakan = Pencairan::with('induk_pencairan')->join('pencairan','pencairan.induk_id','=','induk_pencairan.id')
->where('induk_pencairan.rek_id' ,$rek)
->whereNotIn('induk_id ', [$id])->sum(DB::raw('harga*qty'));
or you can use whereHas
$digunakan = Pencairan::with('induk_pencairan')->whereHas('induk_pencairan',function ($query)use($rek)
{
$query->where('induk_pencairan.rek_id' ,$rek);
})
->whereNotIn('induk_id ', [$id])->sum(DB::raw('harga*qty'));

I solved with this
$digunakan = Pencairan::with('induk_pencairan')
->whereHas('induk_pencairan', function($query) use ($rek) {
$query->where('rek_id', $rek);
})->whereNotIn('induk_id', [$id])->sum(DB::raw('harga*qty'));

Related

Combine whereRaw() with leftjoin() using eloquent

I am trying to join 2 tables and get the latest unique results using whereRaw() and leftJoin() with Laravel eloquent.
I have 2 tables:-
skills table (has timestamps):-
| id| name | icon |
| 1 | skill 1 | skill1.png |
| 2 | skill 2 | skill2.png |
| 3 | skill 3 | skill3.png |
scores table (has timestamps):-
| id| player_id | skill_id | score |
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 |
| 3 | 1 | 3 | 1 |
| 4 | 1 | 2 | 2 |
I would like to return all skills but only the latest entry(by id), so for the above snippet I should get:-
| id| player_id | name | skill_id | score |
| 1 | 1 | skill 1 | 1 | 1 |
| 3 | 1 | skill 3 | 1 | 1 |
| 4 | 1 | skill 2 | 2 | 2 |
I can get the latest unique records by using:
return SkillScores::where('player_id', $this->id)
->whereRaw('id in (select max(id) from skills group by (name))')
->get();
I can get the skill names by using:-
return SkillScores::where('player_id', $this->id)
->leftJoin('skills', 'skill_scores.skill_id', '=', 'skills.id')
->get();
but when I combine them I get an SQLSTATE[23000] error
return SkillScores::where('player_id', $this->id)
->whereRaw('id in (select max(id) from skills group by (name))')
->leftJoin('skills', 'skill_scores.skill_id', '=', 'skills.id')
->get();
Can anyone help me figure out what is going wrong?
EDIT:-
It turns out that the SQLSTATE[23000] error is occuring because I have an id column in both tables and I havent told it which one I am referencing, the below fixes the issue and gives me the correct result.
return SkillScores::where('player_id', $this->id)
->whereRaw('skill_scores.id in (select max(skill_scores.id) from skill_scores group by (skill_id))')
->leftJoin('skills', 'skill_scores.skill_id', '=', 'skills.id')
->get();
I think there is a minor problem on your expected result(id and name is not matching) but i made it work as following;
The query;
SELECT scores.*, skills.*
FROM scores
INNER JOIN (SELECT skill_id, max(id) AS maxId
FROM scores
WHERE player_id = 1
GROUP BY skill_id) AS sub ON sub.maxId = scores.id
INNER JOIN skills ON skills.id = scores.skill_id;
The eloquent version (You may replace it with DB::table() if you want)
$subQuery = Score::where('player_id', DB::raw($this->id))
->groupBy('skill_id')
->select('skill_id', DB::raw('MAX(id) as maxId'));
return Score::join(DB::raw('(' . $subQuery->toSql() . ') as subQuery'), 'subQuery.maxId', '=', 'scores.id')
->join('skills', 'skills.id', '=', 'scores.skill_id')
->get(['scores.*', 'skills.*']);

Select from table where count from another related table is only one

I've tow tables both are related by id ... I want a single query using eloquent or mysql statements to do below ... :
clients
-----------
| Id | name |
-----------
| 1 | name1|
-----------
| 2 | name2|
-----------
| 3 | name3|
-----------
requests
----------------
| Id | client_id |
----------------
| 1 | 1 |
----------------
| 2 | 1 |
----------------
| 3 | 2 |
----------------
| 4 | 3 |
----------------
| 5 | 3 |
----------------
I just want the result to show just clients that has only one request
result
----------------
| Id | name |
----------------
| 2 | name2 |
----------------
How to make it in mysql or laravel elequent ????
You can try the following
DB::table('requests')->groupBy('client_id')->havingRaw('COUNT(*) = 1')->get();
Assuming you have working eloquent Models and relationships you could do:
Client::has('requests', '=', 1)->get();
The next query should resolve your problem:
SELECT
clients.Id,
clients.name
FROM requests
JOIN clients ON clients.Id = requests.client_id
GROUP BY clients.Id, clients.name
HAVING COUNT(*) = 1
;

Laravel Implement greatest-n-per-group

User table:
| id | name | age |
|----|------------|-----|
| 1 | Apple | 22 |
| 2 | Strawberry | 23 |
| 3 | Orange | 50 |
| 4 | Mango | 30 |
Memberships table:
| id | user_id | expire_at |
|----|---------|----------------------|
| 1 | 1 | 2019-08-17T11:19:30Z |
| 2 | 1 | 2019-08-10T11:20:10Z |
| 3 | 2 | 2019-08-29T11:20:19Z |
| 4 | 3 | 2019-08-02T11:20:30Z |
| 5 | 3 | 2019-08-28T11:20:40Z |
Problom
I want select users with the latest 'expire_at'.
After reference: https://stackoverflow.com/a/2111420/5588637,
I tried the following:
SELECT
u.*,
m1.*
FROM
users u
INNER JOIN memberships m1 ON u.id = m1.user_id
LEFT JOIN memberships m2 ON u.id = m2.user_id
AND (
m1.expire_at < m2.expire_at
OR m1.expire_at = m2.expire_at
AND m1.id < m2.id
)
WHERE
m2.id IS NULL;
Result
The id will appear twice because I used to join.
| id | name | age | id | user_id | expire_at |
|----|------------|-----|----|---------|----------------------|
| 1 | Apple | 22 | 1 | 1 | 2019-08-17T11:19:30Z |
| 2 | Strawberry | 23 | 3 | 2 | 2019-08-29T11:20:19Z |
| 3 | Orange | 50 | 5 | 3 | 2019-08-28T11:20:40Z |
After change m1.* to m1.expire_at. I got the result I want.
| id | name | age | expire_at |
|----|------------|-----|----------------------|
| 1 | Apple | 22 | 2019-08-17T11:19:30Z|
| 2 | Strawberry | 23 | 2019-08-29T11:20:19Z |
| 3 | Orange | 50 | 2019-08-28T11:20:40Z |
online try: http://sqlfiddle.com/#!9/27fa22/4
Implement in Lavavel
Laravel Framework version: 5.6.39
I am trying to convert the above SQL into Laravel using Database: Query Builder.
$users = DB::table('users as u')
->select('u.*', 'm1.*')
->join('memberships as m1','u.id','=','m1.user_id')
->leftJoin('memberships as m2', function($join){
$join->on('u.id', '=', 'm2.user_id')
->where(function ($query) {
$query->where('m1.expire_at','<','m2.expire_at')
->orWhere('m1.expire_at','=','m2.expire_at')
->where('m1.id','<','m2.id');
});
})
->whereNull('m2.id')
->toSQL();
I'm using toSql(). This will convert it to SQL first to make sure it's same of above SQL.
SELECT
`u`.*,
`m1`.*
FROM
`users` AS `u`
INNER JOIN `memberships` AS `m1` ON `u`.`id` = `m1`.`user_id`
LEFT JOIN `memberships` AS `m2` ON `u`.`id` = `m2`.`user_id`
AND (
`m1`.`expire_at` < ?
OR `m1`.`expire_at` = ?
AND `m1`.`id` < ?
)
WHERE
`m2`.`id` IS NULL
? seems to be the characteristic of laravel, I believe it is same of above SQL.
when i change toSQL() to get(), the result following:
Collection { ▼
#items: []
}
The above result is wrong, so i tried remove
whereNull('m2.id') in Laravel code (WHERE m2.id IS NULL in SQL), let’s see what happened.
Laravel result
Collection { ▼
#items: array:5 [▼
0 => { ▼
+"id": 1
+"name": "Apple"
+"age": "Eric Yiu SL"
+"user_id": 1
+"expire_at": "2019-08-10T11:20:10Z"
}
...
]
Ideal result
| id | name | age | id | user_id | expire_at |
|----|------------|-----|----|---------|----------------------|
| 1 | Apple | 22 | 2 | 1 | 2019-08-10T11:20:10Z |
| 3 | Orange | 50 | 4 | 3 | 2019-08-02T11:20:30Z |
| 1 | Apple | 22 | 1 | 1 | 2019-08-17T11:19:30Z |
| 2 | Strawberry | 23 | 3 | 2 | 2019-08-29T11:20:19Z |
| 3 | Orange | 50 | 5 | 3 | 2019-08-28T11:20:40Z |
Comparing results, Laravel result missing second id which is memberships table id, i guess this is the reason of incorrect results.
I have searched the Internet, seems is this problem.
https://github.com/laravel/framework/issues/4962
But I failed after various attempts...
You cannot select two rows with the same name in Laravel. The second one will override the first one. Use an alias instead.
$users = DB::table('users as u')
->select('u.*', 'm1.id as membership_id')
->join('memberships as m1','u.id','=','m1.user_id')
->leftJoin('memberships as m2', function($join){
$join->on('u.id', '=', 'm2.user_id')
->where(function ($query) {
$query->whereColumn('m1.expire_at','<','m2.expire_at')
->orWhere(function ($query) {
$query->whereColumn('m1.expire_at','=','m2.expire_at')
->whereColumn('m1.id','<','m2.id');
});
});
})
->whereNull('m2.id')
->get();
Note: I also encapsulated the orWhere() in the join to avoid confusion about the order of AND/OR.
What also works is using a different order in the select. You can for example use the following:
$query->select([
'm1.*',
'm1.id as membership_id',
'u.*'
])
It will return all columns of both tables plus the new membership_id column. But if there is a column on the users table which is named similarly to a column on the memberships table, only the users table column is returned (e.g. created_at). What comes last in your list is returned.
EDIT:
As #Namoshek mentioned, you should not select everything because you have a duplicate key problem in your SQL query. I modified my answer so that it would match #RaymondNijland answer. And by the way, even for the table user, you should select exactly what you need. And not only for a duplicate key problem but also for the speed of your SQL query. We don't think about it enough but it can quickly make the difference on a big set of results.
Less data to send from the database to your PHP server = faster
You should try this one :
DB::table('users as u')
->select('u.*', 'm1.id as membership_id')
->join('memberships as m1','u.id','=','m1.user_id')
->leftJoin('memberships as m2', function ($join) {
$join->on('u.id', '=', 'm2.user_id')
->on(function($join) {
$join->on('m1.id', '<', 'm2.id')
->on(function($join) {
$join->on('m1.expire_at', '<', 'm2.expire_at')
->orOn('m1.expire_at', '=', 'm2.expire_at');
});
});
})
->whereNull('m2.id')
->toSQL()
As mentioned in Laravel's documentation on this page: https://laravel.com/api/5.8/Illuminate/Database/Query/JoinClause.html#method_on
You can pass a closure to the on() method and there is the orOn() method that you can use in this closure.
I tested it and it gives the same result as your SQL query.

FIND_IN_SET with two strings

I have this EMPLOYEE table of employees list
+-----+---------------+-------------+
| ID |EMPLOYEE_ID | SKILLS |
+-----+---------------+-------------+
| 1 | 1 | 3,4 |
+-----+---------------+-------------+
| 2 | 2 | 3,5,2 |
+-----+---------------+-------------+
| 3 | 3 | 1,5 |
+-----+---------------+-------------+
and table POSTED_JOB listing jobs
+-----+---------------+-------------+
| ID |POSTED_JOB_ID | JOB_SKILLS |
+-----+---------------+-------------+
| 1 | 1 | 1,2,3 |
+-----+---------------+-------------+
| 2 | 2 | 3,4 |
+-----+---------------+-------------+
| 3 | 3 | 5,4 |
+-----+---------------+-------------+
| 4 | 4 | 5,6 |
+-----+---------------+-------------+
How can I get all jobs posted with skills corresponding to the skills of employees with laravel query.
For example for employee with employee_id 1, the jobs would be 1,2, and 3.
I tried with find_in_set but here both are lists.
DB::raw("find_in_set(EMPLOYEE.SKILLS , POSTED_JOB.JOB_SKILLS)"), DB::raw(''), DB::raw(''))
$skills = 'select the employee skills';
$skl_arr = explode(',',$skills);
$skl_length = count($skl_arr);
/*query */
$rows->orwhere(DB::raw("find_in_set('$skl_arr[0]','post_job.skills')"));
for ($i=1; $i < $skl_length ; $i++) {
$rows->$join->on(DB::raw("find_in_set('$skl_arr[$i]','post_job.skills')",DB::raw(''),DB::raw('')));
}
You can try this over join
DB::table('POSTED_JOB')->leftJoin('EMPLOYEE', function($join){
$join->on(DB::raw("find_in_set(POSTED_JOB.JOB_SKILLSmEMPLOYEE.SKILLS)"));
});
You can try this to search in two columns.
SELECT * FROM order1 WHERE FIND_IN_SET(order_no,'$foo') OR awb_no IN ('$foo')

Finding sum through eager loading

I've a table schema as follows:
+----+---------------+------------+--------+
| id | crowd_fund_id | email | amount |
+----+---------------+------------+--------+
| 1 | 11 | jj#xx.com | 200 |
| 2 | 11 | sd#ff.com | 250 |
| 3 | 12 | jj#xx.com | 150 |
| 4 | 12 | abc#cc.com | 230 |
+----+---------------+------------+--------+
And a Entries table:
+----+---------+----------+------+
| id | user_id | crowd_id | name |
+----+---------+----------+------+
| 1 | 6 | 11 | Abc |
| 2 | 6 | 12 | Xyc |
| 3 | 8 | 18 | ijn |
+----+---------+----------+------+
In the Backer's Model
public function entry()
{
return $this->belongsTo('App\Entries', 'crowd_fund_id', 'crowd_id');
}
and in the Controller I've called:
$var = Backers::with('entry')->where('email', $user->email)->get();
This works fine. Now I wanted to get the sum also through the eager loading.
That means I need to call something like
Backers::with('entry')->with('sum')->where('email', $user->email)->get();
The sum will calculate the total of all the amount where crowd_fund_id is equal to the raw where email = $user->email.
That means when I call
Backers::with('entry')->with('sum')->where('email', $user->email)->get();
I should be getting :
1 raw for the backer's details with crowd_fund_id
1 raw for the corresponding entry where crowd_fund_id = crowd_id
1 sum of all amount where crowd_fund_id = crowd_fund_id from the backer's details.
How can I get this?
Could you try something along these lines (in your Backers model):
public function backersSum()
{
return $this->hasOne('App\Backer')
->selectRaw('crowd_fund_id, sum(amount) as aggregate')
->groupBy('crowd_fund_id');
}
Doing it this way allows you to eager load it like any relationship. Then you could do something like this to access it:
public function getBackersSumAttribute()
{
if ( ! $this->relationLoaded('backersSum'))
$this->load('backersSum');
$related = $this->getRelation('backersSum');
return ($related) ? (int) $related->aggregate : 0;
}

Categories