Laravel Eloquent where and or between in one query - laravel

Hello what is the Laravel eloquent code for this:
SELECT *
FROM dtrs
inner join emp_informations
on dtrs.enrollnum = emp_informations.enrollnum
where (emp_informations.EmpID = '07081408')
and (date_in between '2015-03-05' and '2015-03-20' and
date_out between '2015-03-05' and '2015-03-20')
ORDER BY dtrs . date_in DESC
Here's my code in eloquent, and it doesn't show the same result with the original query.
$dtrs=Dtr::Join('emp_informations','dtrs.enrollnum','=','emp_informations.enrolnum')
->where('emp_informations.first_name','like',"%$id%")
->orWhere('emp_informations.last_name','like',"%$id%")
->whereBetween('dtrs.date_in', array('2015-03-05' , '2015-03-20'))
->orderBy('dtrs.date_in','Desc')->paginate(15);
return view('timetrack' , compact('dtrs'));
What is wrong with my code?

I have changed somethings in your query, please try this :
$dtrs=Dtr::Join('emp_informations','dtrs.enrollnum','=','emp_informations.enrolnum')
->where('emp_informations.EmpID','=','07081408')
->whereBetween('dtrs.date_in', array('2015-03-05' , '2015-03-20'))
->whereBetween('dtrs.date_out', array('2015-03-05' , '2015-03-20'))
->orderBy('dtrs.date_in','Desc')->paginate(15);
if you have variables, you can make like this:
->whereBetween('dtrs.date_in', array($fromdate , $todate))

Related

Laravel create Query SQL

I need help to create my query.
I created the following query in SQL
SELECT id_funcionario, MAX(created_at) from irs GROUP by id_funcionario
I try to switch to Larevel but I'm wrong and I can't put the groupby
$irs = irs::join('utilizadors','utilizadors.id','=', 'irs.id_funcionario')
->MAX('created_at')
->get([ 'utilizadors.id', 'utilizadors.nome', 'irs.id', 'irs.id_funcionario','irs.created_at', 'irs.dependentes', 'irs.titulares_rendimento', 'irs.situacoes_especiais']);
return view('irs-total', ['itens' => $irs]);
Try this:
use Illuminate\Support\Facades\DB;
$irs = DB::table('irs')
->join('utilizadors','utilizadors.id','=', 'irs.id_funcionario')
->select(DB::raw('irs.created_at'), 'utilizadors.id',
'utilizadors.nome', 'irs.id', 'irs.id_funcionario','irs.created_at',
'irs.dependentes', 'irs.titulares_rendimento',
'irs.situacoes_especiais')
// add your groupBy data
->groupBy('')
->get();

how to convert query this query to laravel

SELECT *
FROM `digital_useraccess`
left join digital_publisher ON
access_publisher = pub_auto
left join digital_issue ON
issue_publisher = pub_auto
WHERE 1
without using laravel DB class method
Try this as reference
ModelName::where(conditions)
->leftjoin('digital_publisher','access_publisher','pub_auto')
->leftjoin('digital_issue','issue_publisher ','pub_auto')
->select('column_1','column_2',...)->get();
This might help :
::query()->where('<cplumn>' , '<operator>' ,
'<value>')->leftJoin('digital_publisher', 'access_publisher',
'=','pub_auto')->leftJoin('digital_issue', 'issue_publisher', '=',
'pub_auto')->select('<columns>')->get();/find();/first();

How to write subquery like this using active records

Hello friends I wanna write this query in codeigniter using active record,
public function getHallServices($city,$receiption_capacity,$booked_date){
$sql="select hi.*,hf.* FROM hall_info hi, hall_feature hf WHERE
hf.hall_info_id=hi.id and hi.city=? and hf.receiption_capacity<=? and hi.id not IN
(select hall_info_id FROM events
WHERE event_date = ?
GROUP BY hall_info_id
)";
$query=$this->db->query($sql,array('city'=>$city,'receiption_capacity'=>$receiption_capacity,'event_date'=>$booked_date));
return $query->result_array();
}
This query is not working as expected in codeigniter but works fine in postresql. I am not getting how to pass arguments to inner query. Please help me to correct or can give active record query for same.
Modify your codes to be like this:
public function getHallServices($city,$receiption_capacity,$booked_date){
$sql="select hi.*,hf.* FROM hall_info hi, hall_feature hf WHERE
hf.hall_info_id=hi.id and hi.city=? and hf.receiption_capacity<=? and hi.id not IN
(select hall_info_id FROM events
WHERE event_date = ?
GROUP BY hall_info_id
)";
$query=$this->db->query($sql,array($city,$receiption_capacity,$booked_date));
return $query->result_array();
}
Tell me if it works.

Query date field in Yii2

In SQL, I might do something like this
"WHERE DATEPART(month, my_date_field) = 8"
to grab the rows where month in my_date_field = 8.
I am unsure how this syntax translates into Yii2.
I have
$query = Fees::find();
$fees = $query->all();
How do I use the WHERE clause on the date field within that $query ?
The query in yii2 allows text in where clause as below
$fees = Fees::find()
->where("DATEPART(month, my_date_field) = 8")
->all();
You may chain other 'sql functions' like order, group by , limit etc like this as well.

Symfony2 subquery within Doctrine entity manager

I need to perform this query:
SELECT * FROM (SELECT * FROM product WHERE car = 'large' ORDER BY onSale DESC) AS product_ordered GROUP BY type
In Symfony2 using the entity manager.
My basic query builder would be :
$query = $em->getRepository('AutomotiveBundle:Car')
->createQueryBuilder('p')
->where('pr.car = ?1')
->andWhere('pr.status = 1')
->orderBy('pr.onSale', 'DESC')
->setParameter(1, $product->getName())
->groupBy('p.type')
->getQuery();
But I cannot work out how to add in a subquery to this.
Ive tried making a separate query and joining it like:
->andWhere($query->expr()->in('pr.car = ?1',$query2->getQuery()));
But I get:
Call to undefined method Doctrine\ORM\Query::expr()
One trick is to build two queries and then use getDQL() to feed the first query into the second query.
For example, this query returns a distinct list of game ids:
$qbGameId = $em->createQueryBuilder();
$qbGameId->addSelect('distinct gameGameId.id');
$qbGameId->from('ZaysoCoreBundle:Event','gameGameId');
$qbGameId->leftJoin('gameGameId.teams','gameTeamGameId');
if ($date1) $qbGameId->andWhere($qbGameId->expr()->gte('gameGameId.date',$date1));
if ($date2) $qbGameId->andWhere($qbGameId->expr()->lte('gameGameId.date',$date2));
Now use the dql to get additional information about the games themselves:
$qbGames = $em->createQueryBuilder();
$qbGames->addSelect('game');
$qbGames->addSelect('gameTeam');
$qbGames->addSelect('team');
$qbGames->addSelect('field');
$qbGames->addSelect('gamePerson');
$qbGames->addSelect('person');
$qbGames->from('ZaysoCoreBundle:Event','game');
$qbGames->leftJoin('game.teams', 'gameTeam');
$qbGames->leftJoin('game.persons', 'gamePerson');
$qbGames->leftJoin('game.field', 'field');
$qbGames->leftJoin('gameTeam.team', 'team');
$qbGames->leftJoin('gamePerson.person', 'person');
// Here is where we feed in the dql
$qbGames->andWhere($qbGames->expr()->in('game.id',$qbGameId->getDQL()));
Kind of a long example but i didn't want to edit out stuff and maybe break it.
You can use DBAL for performing any sql query.
$conn = $this->get('database_connection');//create a connection with your DB
$sql="SELECT * FROM (SELECT * FROM product WHERE car =? ORDER BY onSale DESC) AS product_ordered GROUP BY type"; //Your sql Query
$stmt = $conn->prepare($sql); // Prepare your sql
$stmt->bindValue(1, 'large'); // bind your values ,if you have to bind another value, you need to write $stmt->bindValue(2, 'anothervalue'); but your order is important so on..
$stmt->execute(); //execute your sql
$result=$stmt->fetchAll(); // fetch your result
happy coding

Resources