Eloquent Query Building - php

I would like to know how can i transform this query into eloquent. Im having some troubles displaying those "counts" fields.
$resultados = DB::select( DB::raw("
Select * From (
Select
P.*,
count(distinct PP.id) as countProduct,
count(distinct C.id) as countCity,
count(distinct CO.id) as countCountry,
count(distinct CON.id) as countContinent
From
packages P
Inner join citie_package CP
on P.id = CP.package_id
Inner join cities C
on CP.city_id = C.id
Inner Join countries CO
on C.country_id = CO.id
Inner join continents CON
on CO.continent_id = CON.id
Inner Join product_package PP
on P.id = PP.package_id
where
$whereCondition
group by
P.id,
P.name,
P.image,
P.days,
P.operator_id,
P.badge,
P.price,
P.brochure,
P.lodging,
P.itinerary,
P.services,
P.observations,
P.offer,
P.featured,
P.validity,
P.created_at,
P.updated_at,
P.invisible,
P.operationdates
) as pepe
where
$subWhereCondition
"));
For now the query is working good (on my localhost), when i uploaded it to production i found the following problem:
"SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
So im deciding to built it again but on this time using eloquent.
Thanks!!
Edit:
Before i tried something like this:
$resultado = DB::table('packages')
->join('product_package','packages.id','=','product_package.package_id')
->join('products','products.id','=','product_package.product_id')
->join('citie_package','packages.id','=','citie_package.package_id')
->join('cities','cities.id','=','citie_package.city_id')
->join('countries','countries.id','=','cities.country_id')
->join('continents','continents.id','=','countries.continent_id')
->where(function($resultado) use ($input){
if(isset($input['products'])){
$resultado->whereIn('products.id',$input['products']);
}
if(isset($input['cities'])){
$resultado->whereIn('cities.id',$input['cities']);
}
if(isset($input['countries'])){
$resultado->whereIn('countries.id',$input['countries']);
}
if(isset($input['continents'])){
$resultado->whereIn('continents.id',$input['continents']);
}
if(isset($input['operator'])){
$resultado->whereIn('packages.operator_id',$input['operator']);
}
})
->where('days', '>=', $input['durationFrom'])
->where('days', '<=', $input['durationTo'])
->where('price', '>=', str_replace('$','',str_replace('.','',$input['costFrom'])))
->where('price', '<=', str_replace('$','',str_replace('.','',$input['costTo'])))
->select(
array(
DB::raw('packages.*'),
DB::raw('COUNT(distinct cities.id) as count')
)
)->get()
return $resultado;
Using groupBy!
Finally i figured it out :D:
This is the final code
$resultados = Package::
join('product_package','packages.id','=','product_package.package_id')
->join('products','products.id','=','product_package.product_id')
->join('citie_package','packages.id','=','citie_package.package_id')
->join('cities','cities.id','=','citie_package.city_id')
->join('countries','countries.id','=','cities.country_id')
->join('continents','continents.id','=','countries.continent_id')
->where(function($resultado) use ($input){
if(isset($input['products'])){
$resultado->whereIn('products.id',$input['products']);
}
if(isset($input['cities'])){
$resultado->whereIn('cities.id',$input['cities']);
}
if(isset($input['countries'])){
$resultado->whereIn('countries.id',$input['countries']);
}
if(isset($input['continents'])){
$resultado->whereIn('continents.id',$input['continents']);
}
if(isset($input['operator'])){
$resultado->whereIn('packages.operator_id',$input['operator']);
}
})
->groupBy('packages.id')
->where('days', '>=', $input['durationFrom'])
->where('days', '<=', $input['durationTo'])
->where('price', '>=', str_replace('$','',str_replace('.','',$input['costFrom'])))
->where('price', '<=', str_replace('$','',str_replace('.','',$input['costTo'])))
->select( array(
DB::raw('packages.*'),
DB::raw('COUNT(distinct cities.id) as CountCity ,COUNT(distinct continents.id) as CountContinent, COUNT(distinct countries.id) as CountCountry, COUNT(distinct products.id) as CountProduct') ) )
->get();
if(isset($input['cities'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountCity == count($input['cities']);
});
}
if(isset($input['countries'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountCountry == count($input['countries']);
});
}
if(isset($input['continents'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountContinent == count($input['continents']);
});
}
if(isset($input['products'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountProduct == count($input['products']);
});
}
Thanks!

Related

mysql query to check a field exist in an array in codeigniter

I post an array of skills from the form and i need to fetch list of candidates list related to the elements in array in codeigniter.
Controller
$data = array(
"mode" => $this->input->post("mode"),
"designation" => $this->input->post("designation"),
"notice_period" => $this->input->post("notice_period"),
"skill" => $this->input->post("skills"),
"cities" => $this->input->post("city")
);
$this->load->model("search_model");
$this->load->model("candidate_model");
$data["candidate"] = $this->search_model->fetch_candidate($data);
$data["designation"] = $this->candidate_model->fetch_designation();
$this->load->view('candidate_profiles', $data);
model
function fetch_candidate($data)
{
$mode = $data['mode'];
$designation = $data['designation'];
$notice_period = $data['notice_period'];
$skill = $data['skill'];
$skills = join(",",$skill);
$city = $data['cities'];
$query = $this->db->query("SELECT DISTINCT(U.user_id), U.firstname, U.lastname, U.username, U.usertype, U.email, U.phone, U.address, U.profile_image, C.name AS country,C.id AS countryid, S.name AS state,S.id AS stateid, A.housename, A.street, A.area, A.po, A.city, CJ.designation, CJ.resume, CJ.biography, CJ.hiring_mode, CJ.notice_period, CJ.current_CTC, CJ.expected_CTC, D.designation_id AS desig, D.designation AS desig_name
FROM users AS U
LEFT JOIN address AS A ON U.user_id=A.cand_id
LEFT JOIN cand_job_details AS CJ ON CJ.cand_id=U.user_id
LEFT JOIN designations AS D ON D.designation_id=CJ.designation
LEFT JOIN countries AS C ON C.id=A.country
LEFT JOIN states AS S ON S.id=A.state
LEFT JOIN user_skills AS US ON U.user_id=US.user_id
LEFT JOIN user_interested_cities AS UC ON U.user_id=UC.user_id
WHERE D.designation LIKE '$designation' && (CJ.hiring_mode='2' || CJ.hiring_mode='$mode') && CJ
.notice_period<='$notice_period' && UC.city='$city' && US.skill IN(".implode(",", $skills).")
");
return $query;
}
How to implement this on codeigniter?
Thanks in advance
You need try to add ' around each argument. After it should looks like IN ('PHP','AngularJS'), because it is a string value and datatype is varchar as well.
$skills = "'";
foreach($skill as $item) {
$skills .= $item."',";
}
$skills = trim($skills,",")
Codeigniter comes with a built in Querybuilder - i strongly suggest to use it, because it makes your life much easier, and protects you against SQL injections - which is clearly a problem in your case.
Try the following
function fetch_candidate($data)
{
$this->db
->select('SELECT DISTINCT(U.user_id), U.firstname, U.lastname, U.username, U.usertype, U.email, U.phone, U.address, U.profile_image, C.name AS country,C.id AS countryid, S.name AS state,S.id AS stateid, A.housename, A.street, A.area, A.po, A.city, CJ.designation, CJ.resume, CJ.biography, CJ.hiring_mode, CJ.notice_period, CJ.current_CTC, CJ.expected_CTC, D.designation_id AS desig, D.designation AS desig_name')
->from('users U')
->join('address A', 'U.user_id=A.cand_id', 'left')
->join('cand_job_details CJ', 'CJ.cand_id=U.user_id', 'left')
->join('designations D', 'D.designation_id=CJ.designation', 'left')
->join('countries C', 'C.id=A.country', 'left')
->join('states S', 'S.id=A.state', 'left')
->join('user_skills US', 'U.user_id=US.user_id', 'left')
->join('user_interested_cities UC', 'U.user_id=UC.user_id', 'left');
if (isset($data['designation']) && !empty($data['designation']))
{
$this->db->like('D.designation', $data['designation']);
}
if (isset($data['mode']) && !empty($data['mode']))
{
$this->db
->group_start()
->where('CJ.hiring_mode', 2)
->or_where('CJ.hiring_mode', $data['mode'])
->group_end();
}
if (isset($data['notice_period']) && !empty($data['notice_period']))
{
$this->db->where('CJ.notice_period <=', $data['notice_period']);
}
if (isset($data['city']) && !empty($data['city']))
{
$this->db->where('UC.city', $data['city']);
}
if (isset($data['skills']) && is_array($data['skills']))
{
$this->db->where_in('US.skill', $data['skills']);
}
return $this->db->get();
}

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();

Query from two tables laravel with multiple conditions

I'm trying to write this query in Laravel query
SELECT
table1.*, table2.*
FROM
table1
LEFT JOIN
table2
ON
(table1.id = table2.id AND (table2.field = '' OR table2.field >= '0'))
WHERE
table.id = id
I have problem with how to add inner part AND ( ... ) to the query? Here is what I have so far
$query = Table::select(
DB::Raw('table1.*, table2.*'))
->leftJoin('table2', function($join) {
$join->on('table1.id', '=', 'table2.id')
->where('table2.field', '=', '')
->orwhere('table2.field', '=', '50');
})->where('table1.id', BaseController::getCurrentUser()->id)
->get();
I miss where and how to add AND ...
Do you need to have the AND part as part of ON? If you move it to the WHERE part of your query you could use something like:
$query = Table::select('table1.*', 'table2.*')
->leftJoin('table2', 'table1.id', '=', 'table2.id')
->where('table1.id', BaseController::getCurrentUser()->id)
->where(function ($query){
$query->where('table2.field', '')
->orWhere('table2.field', 50)
});
The result should be the same as from your original query.

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();

Categories