laravel query builder : I have mysql query - laravel

I have the following MySQL query that I want to convert to a Laravel 5.8 query builder. How could I do this?
SELECT
u.id,
u.username,
(select count(*) as invalid_count from invalid_template_messaging where user_id = u.id) as t1,
(select count(*) as valid_count from template_messagings where user_id = u.id) as t2 FROM
users as u GROUP BY
u.id

You can write this query like that
DB::select(`u.id`,`u.username`)
->selectSub(`select count as invalid_count from invalid_template_messaging where user_id = u.id`, `t1`)
->selectSub(`select count as valid_count from template_messagings where user_id = u.id`, `t2`)
->from(`users as u`)
->groupBy(`u.id`)
->get();

Related

how to write query with "with" using laravel eloquent

How to make with laravel a query with with like the example below
Thank you
with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
WITH clauses are not supported by Eloquent. You can either pass in a raw query
$sql = <<<SQL
with s as (select s.id from s)
select a.*
from a
left join s on s.id = a.s_id
...
SQL;
$results = DB::select($sql);
Or use a package that adds this functionality like staudenmeir/laravel-cte
DB::query()
->withExpression('s', fn ($query) => $query->select('s.id')->from('s'))
->select('a.*')
->from('a')
->leftJoin('s', 's.id', 'a.s_id')
...

laravel query to make pagination

$users = SELECT users.id,
users.name,
cd.title,
cd.updated_at,
profiles.avatar
FROM `users`
JOIN (SELECT Max(id) max_id,
user_id
FROM book
GROUP BY user_id) c_max
ON ( c_max.user_id = users.id )
JOIN book cd
ON ( cd.id = c_max.user_id )
JOIN profiles
ON ( profiles.user_id = users.id )
ORDER BY cd.updated_at DESC
i am using this to get data in laravel query
$users = DB::select(DB::raw('SELECT users.id, users.name, cd.title, cd.updated_at, profiles.avatar FROM users JOIN ( SELECT MAX(id) max_id, user_id FROM book GROUP BY user_id )
c_max ON (c_max.user_id = users.id) JOIN book cd ON (cd.id = c_max.user_id) JOIN profiles ON (profiles.user_id = users.id) ORDER by cd.updated_at DESC limit 24 offset 0'));

How to group by with union in laravel

I have two table that I want to union and after I union them I want to groupBy the one column that I used in union
Here is what I tried:
$issuance = DB::table('issuance as i')
->select('i.issue_to as stud_id', 'i.created_on');
$stud= DB::table('transfer as t')
->select('t.transfer_to as stud_id', 't.created_on')
->union($issuance)
->select('stud_id', 'created_on', DB::raw('COUNT(*) as total_asset'))
->groupBy('stud_id')
->orderBy('created_on', 'DESC')->get();
This is the MySQL query in what I tried
"(select `stud_id`, `created_on`, COUNT(*) as total_asset from `transfer` as
`t` group by `stud_id`) union (select `i`.`issued_to` as `stud_id`, `i`.`created_on` from
`issuance` as `i`) order by `created_on` desc"
What I really want in MySQL is like this:
select stud_id, count(*) from ((select `t`.`transfered_to` as `stud_id`,
`t`.`created_on` from `transfer` as `t`) union (select `i`.`issued_to` as
`stud_id`, `i`.`created_on`, COUNT(*) as asset from `issuance` as `i`)) as t
group by stud_id order by `created_on` desc
Thank you for the help
//try this example ,it will helps you
$query1 = DB::table('table1')->where(....);
$query2 = DB::table('table2')->where(....);
$data = DB::select(DB::raw('id, MAX(created_at) as max_created_at')>union($query1)->union($query2)->orderBy('max_created_at')->groupBy('id')->get();

How to join on a select result with eloquent in Laravel?

I want to join on a result of other select like this :
SELECT *
FROM TABLE1
JOIN (
SELECT cat_id FROM TABLE2 where brand_id = 2 GROUP BY TABLE2.cat_id) AS b ON TABLE1.id = b.cat_id
is there any way to do this with eloquent?
As it is mentioned here, using DB:raw() will solve your problem.
DB::table('table1')->join(DB::raw("(SELECT
cat_id
FROM table2
WHERE brand_id = 2
GROUP BY table2.cat_id
) as b"),function($join){
$join->on("b.cat_id","=","table1.id");
})->get();
\DB::table('table1')->join('table2' , function($join){
$join->on('table1.id', '=', 'table2.cat_id');
})->select(['table2.cat_id' , 'table1.*'])
->where('table2.brand_id' , '=' , '2')
->groupBy('table2.cat_id');
Depends on whether brand_id is in table1 or table2
You can also use model approach for it.
TABLE1::join('table2' , function($join){
$join->on('table1.id', '=', 'table2.cat_id');
})->select(['table2.cat_id' , 'table1.*'])
->where('table2.brand_id' , '=' , '2')
->groupBy('table2.cat_id');

Right outer join on two tables where ON clause has a subquery

I am trying to perform a right outer join on two liferay tables — users_ and expandovalue — to get a result set.
When I did the following query on all the users, I got desired result.
SELECT USER_.FIRSTNAME
, USER_.LASTNAME
, USER_.EMAILADDRESS
, USER_.JOBTITLE
, expandovalue.data_
from expandovalue
right outer join
user_ on expandovalue.classpk = user_.userid
and expandovalue.columnid =35695;
When tried to do the same for a set of users (part of a user group) it errored out. Query below:
SELECT USER_.FIRSTNAME
, USER_.LASTNAME
, USER_.EMAILADDRESS
, USER_.JOBTITLE
, expandovalue.data_
from expandovalue
right outer join
user_ on expandovalue.classpk
in (select userid
from user_
where userid
in ( select userid
from users_usergroups
where usergroupid = 40073
) and status =0) = user_.userid in
(select userid
from user_
where userid
in ( select userid
from users_usergroups
where usergroupid = 40073
)
and status =0
) and expandovalue.columnid =35695
Here's the subquery which gives the userids of people in a particular usergroup.
(select userid
from user_
where userid in
( select userid
from users_usergroups
where usergroupid = 40073)
and status =0
)
Am I going in a completely incorrect direction? Please advise.
Try this query:
SELECT USER_.FIRSTNAME,
USER_.LASTNAME,
USER_.EMAILADDRESS,
USER_.JOBTITLE,
expandovalue.data_
from expandovalue
right outer join user_
on expandovalue.classpk IN
(select userid
from user_
where userid in (select userid
from users_usergroups
where usergroupid = 40073) and
status = 0) AND
user_.userid in (select userid
from user_
where userid in (select userid
from users_usergroups
where usergroupid = 40073) AND
status =0) AND
expandovalue.columnid = 35695
There was an "AND" missing after the end of the first subquery.

Resources