laravel query for bewteen - php

I want to convert the sql query to laravel query which is working fine in sql
select
a.id,
a.assigned_user_name,
a.lead_stage_c,
a.meeting_start_date,
a.call_start_date
from
mgl_report_targets as a
left join
dcalendardate b
on b.datevalue = a.meeting_start_date
left join
dcalendardate c
on c.datevalue = a.call_start_date
where
(
a.lead_stage_c = 'Target-MQL'
)
and
(
b.datevalue between '2020-07-31' and '2020-09-01'
)
or
(
c.datevalue between '2020-07-31' and '2020-09-01'
)
and a.active_status = '1'
and a.lead_stage_c = 'Target-MQL'
In laravel I have tried as
$report_details=DB::table('mgl_report_targets')->where('active_status',1);
$report_details=$report_details->leftjoin('dcalendardate as a','a.datevalue','=',DB::raw("mgl_report_targets.meeting_start_date::date"))->leftjoin('dcalendardate as b','b.datevalue','=',DB::raw("mgl_report_targets.call_start_date::date"));
$report_details=$report_details->where(function ($query) {
return $query->whereBetween('a.datevalue',['2020-07-31','2020-09-01']);
})
->orWhere(function ($query) {
return $query->whereBetween('b.datevalue',['2020-07-31','2020-09-01']);
});
$report_details=$report_details->where('mgl_report_targets.lead_stage_c','=','Target-MQL')->select('mgl_report_targets.id','mgl_report_targets.assigned_user_name','mgl_report_targets.meeting_start_date','mgl_report_targets.call_start_date','mgl_report_targets.lead_stage_c')->get();
but between query not query as expected.
What I did wrong.

Related

how to write sql query to laravel

i have a sql query like this and i want to convert it to laravel how i can do it?
i have try it, but i confusing on wherein and join
sql query
SELECT MIN(StartFrom) as StartFrom,MAX(EndTo) as EndTo from appointmentsettings
WHERE day=1
and PersonID IN (
SELECT p.id
FROM users p
JOIN appointmentsettings aps ON p.id = aps.PersonID
WHERE p.active=1 AND aps.CompanyID = 1 OR aps.PersonID IN(
SELECT cps.user_id
from companypersonstructs cps
WHERE cps.CompanyID =1
) group by aps.PersonID
)
and active=1
here what i try
Appointmentsetting::select('StartFrom', 'EndTo')
->min('StartFrom')
->max('EndTo')
->where(['Day'=>$day, 'Active'=>1])
->whereIn('PersonID', function ($query) use ($id) {
$query->select('p.id')
->from('users as p')
$query->join('appointmentsettings as aps', 'p.id', '=', '')
->where(["user_id" => $id, 'Active' => 1])->get();
})->orderBy('id')->get();
Appointmentsetting::select('StartFrom', 'EndTo')
->min('StartFrom')
->max('EndTo')
->where(['Day'=>$day, 'Active'=>1])
->whereIn('PersonID', function ($query) use ($id) {
$query->select('p.id')
->from('users as p')
->join('appointmentsettings as aps', 'p.id', '=', '')
->where(["user_id" => $id, 'Active' => 1]);
})->orderBy('id')->get();
You have an error in your join inside the function. Try the above code.
Also you must use get() only at the end of the query and not inside the function itself.
DB Query way
DB::table('appointmentsettings')->join('users','p.id','=','')
->select(DB::raw('MIN(StartFrom) as StartFrom','Max(EndTo) as EndTo'))
->where([['user_id',$id],['Active','1']])
->groupBy('appointmentsettings.PersonID)
->orderBy('id','ASC')
->get();

How to effectively using laravel 5.5 eloquent on complex join and group

I have this query that successfully run on mysql workbench:
SELECT
any_value(u.username),
any_value(b.name),
any_value(gc.visit_cycle_id),
any_value(gc.group_number),
any_value(dc.total_customer),
count(*) as total_day
FROM
trackgobackenddb.group_cycles gc
LEFT JOIN (
SELECT
any_value(dc.group_cycle_id) as group_cycle_id,
count(*) as total_customer
FROM
trackgobackenddb.destination_cycles dc
GROUP BY dc.group_cycle_id
) dc ON dc.group_cycle_id = gc.id
LEFT JOIN visit_cycles vc ON gc.visit_cycle_id = vc.id
LEFT JOIN users u ON vc.user_id = u.id
LEFT JOIN branches b ON vc.branch_id = b.id
GROUP BY gc.visit_cycle_id, gc.group_number;
How to convert that query so I can use laravel eloquent or query builder?
You can use DB::select to run complex raw SQL
$sql = "";
$data = DB::select($sql);
Here's my untested attempt using the query builder:
DB::table('group_cycles AS gc')
->leftJoin('destination_cycles AS dc', function ($join) {
$join->on('dc.group_cycle_id', '=', 'gc.id')
->select(['dc.group_cycle_id AS group_cycle_id', DB::raw('select count(*) AS total_customer')]);
})
->leftJoin('visit_cycles AS vc', function ($join) {
$join->on('gc.visit_cycle_id', '=', 'vc.id');
})
->leftJoin('users AS u', function ($join) {
$join->on('vc.user_id', '=', 'u.id');
})
->leftJoin('branches AS b', function ($join) {
$join->on('vc.branch_id', '=', 'b.id');
})
->groupBy('gc.visit_cycle_id', 'gc.group_number')
->get();

Laravel nested join queries with query builder

This is a search function that returns each member with their most recent year registered.
I got it working with a DB::raw() call. But can't get it working with the query builder.
Working Code:
$query = DB::table('membership as m');
$query->join(
DB::raw(
'(SELECT my.*
FROM membership_years my
INNER JOIN (
SELECT member_id,MAX(membership_year) AS max_my
FROM membership_years
GROUP BY member_id
) my2
ON my.member_id = my2.member_id
AND my.membership_year = my2.max_my
) my'
)
,'m.id','=','my.member_id');
My attempt on query builder code:
$query = DB::table('membership as m');
$query->join('membership_years as my',
function($j1){
$j1->join('membership_years as my2',
function($j2){
$j2->where('my.membership_year','=','MAX(my2.membership_year)')
->on('my.member_id','=','my2.member_id');
}
)->on('m.id','=','my.member_id');
}
);
The resulting error is:
Call to undefined method Illuminate\Database\Query\JoinClause::join()
I'm not sure if this is because the $j2 doesn't have access to the join method anymore?
Raw MySQL query:
SELECT my.membership_year,m.*
FROM membership AS m
INNER JOIN
(
SELECT my1.*
FROM membership_years my1
INNER JOIN
(
SELECT member_id,MAX(membership_year) AS max_my
FROM membership_years
GROUP BY member_id
) my2
ON my1.member_id = my2.member_id
AND my1.membership_year = my2.max_my
) my
ON m.id = my.member_id
ORDER BY m.id ASC
Way 1. You can write part of the query with builder:
$query = DB::table('membership as m')
->select('my.membership_year', 'm.*')
->join(DB::raw('(
SELECT my1.*
FROM membership_years my1
INNER JOIN (
SELECT member_id, MAX(membership_year) AS max_my
FROM membership_years
GROUP BY member_id
) my2
ON my1.member_id = my2.member_id
AND my1.membership_year = my2.max_my
) my'),
'm.id', '=', 'my.member_id')
->orderBy('m.id');
Way 2. Also you can write subqueries and use toSql() method:
$sub1 = DB::table('membership_years')
->select('member_id', DB::raw('MAX(membership_year) AS max_my'))
->groupBy('member_id');
$sub2 = DB::table('membership_years as my1')
->select('my1.*')
->join(DB::raw('(' . $sub1->toSql() . ') my2'),
function ($join) {
$join
->on('my1.member_id', '=', 'my2.member_id')
->on('my1.membership_year', '=', 'my2.max_my');
});
$query = DB::table('membership as m')
->select('my.membership_year', 'm.*')
->join(DB::raw('(' . $sub2->toSql() . ') my'), 'm.id', '=', 'my.member_id')
->orderBy('m.id');

How to do this JOIN query in Laravel?

I'm trying to do this join in laravel query builder but it is throwing and error. I'm using and Mysql DB.
This is my sql:
select c.*, d.* from notas_cabecera c
join notas_detalle d on (c.codigo_nota = d.codigo_nota)
where c.codigo_nota in (select r.codigo_nota from reportes r);
And this is my laravel query:
$lista_reportes = DB::table('notas_cabecera')
->join('notas_detalle', 'notas_cabecera.codigo_nota', '=', 'notas_detalle.codigo_nota')
->whereIn('notas_cabecera.codigo_nota', function($query)
{
$query->select(
DB::table('reportes')->select('reportes.codigo_nota')
);
})
->get();
What I'm doing wrong? please help.
$lista_reportes = DB::table('notas_cabecera')
->join('notas_detalle', 'notas_cabecera.codigo_nota', '=', 'notas_detalle.codigo_nota')
->whereIn('notas_cabecera.codigo_nota', function($query)
{
$query
->select('reportes.codigo_nota')
->from('reportes');
})
->get();

How to write this query in laravel 4 using query builder

update `access_tokens` as at3 set at3.`expires` = 1393995576
where
`access_token_expires` > 1393995576
and at3.`id` in (select * from (
select `at2`.`id` from `refresh_tokens` as `rt`
left join `access_tokens` as `at` on `at`.`id` = `rt`.`access_token_id`
inner join `access_tokens` as `at2` on `at2`.`session_id` = `at`.`session_id`
where `rt`.`refresh_token` = 's2kF5Ev6NXncnTPwVz99ksgsCGXfwPIDzXJMZJqz') as t)
I am trying above query in laravel 4 using query builder. But not able to find how to write this. I have written upto this:
DB::table("access_tokens as at3")
->whereIn('id', function($query) use ($authParams)
{
$query->select('at2.id')
->from('refresh_tokens as rt')
->leftJoin("access_tokens as at", "at.id", "=", "rt.session_access_token_id")
->join("access_tokens as at2", "at2.session_id", "=", "at.session_id")
->where('rt.refresh_token', '=', $authParams['refresh_token']);
})
//->delete();
->where('at3.access_token_expires', '>', time())
->update(array('at3.access_token_expires' => time()));
If you just need the query to work, you can use this syntax:
$affected_rows = DB::update( DB::raw("UPDATE users set username = 'wadus' WHERE username = :somevariable"), array('somevariable' => $someVariable));
It works and it's safe.

Categories