how to convert query this query to laravel - 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();

Related

Delete Join in query builder Laravel

I have a delete query like this:
DELETE prefix_tableA, prefix_tableB, prefix_tableC
FROM prefix_tableA
INNER JOIN prefix_tableB ON prefix_tableA.user_id = prefix_tableB.user_id
INNER JOIN prefix_tableC ON prefix_tableB.user_id = prefix_tableC.user_id
WHERE prefix_tableA.id = 148
Now I want to convert it to query builder. I searched a lot of posts here but still not found expected answer.
I don't want to use DB::raw because I have prefix in my table name. When I change the prefix, my query will error.
Anyway to write this in query builder?
Thank you!
You can do this using DB::delete() or DB::statement()
Try this query :
\DB::delete('DELETE prefix_tableA, prefix_tableB, prefix_tableC FROM prefix_tableA, prefix_tableB, prefix_tableC INNER JOIN prefix_tableB ON prefix_tableA.user_id = prefix_tableB.user_id INNER JOIN prefix_tableC ON prefix_tableB.user_id = prefix_tableC.user_id WHERE prefix_tableA.id = 148');
Try this Query your Controller
DB::table('prefix_tableA')->join('prefix_tableB','prefix_tableA.user_id','=','prefix_tableB.user_id')->join('prefix_tableC','prefix_tableB.user_id','=','prefix_tableC.user_id')->where('prefix_tableA.id',148)->delete('prefix_tableA', 'prefix_tableB', 'prefix_tableC');
Thanks for your reply guys!
Like I said, I config my table prefix in config/database.php, so use raw query will be error if I change the prefix name in that file.
So my only way for me to do this is write a delete query in each single table and call it in my controller. I implemented about 3 model, long code, but it's the only way in Laravel!
You should try as this way. This way works form me.
$raw_query = 'DELETE prefix_tableA, prefix_tableB, prefix_tableC
FROM prefix_tableA
INNER JOIN prefix_tableB ON prefix_tableA.user_id = prefix_tableB.user_id
INNER JOIN prefix_tableC ON prefix_tableB.user_id = prefix_tableC.user_id
WHERE prefix_tableA.id = ?';
$status = \DB::delete($raw_query, array('148'));
Documentation: http://laravel.com/docs/database#running-queries

How to make and condition with or in laravel query

My query is as below
SELECT * FROM `user_register`
INNER JOIN `locationdetail` on `locationdetail`.`userid` = `user_register`.`id`
INNER JOIN `lifestyle` on `lifestyle`.`userid` = `user_register`.`id`
WHERE `lifestyle`.`drink` in (2) and
( `locationdetail`.`state_id` in (4121) or `locationdetail`.`country_id` in (38))
In this query how to make bracket inner query in laravel model
Sorry there got distracted
$drinks = [1];
$state_ids = [1,2]
$country_ids = [1,2,3]
$someModel
->join() // enter your joins here
->whereIn('lifestyle.drink', $drinks)
->where(function( $q1 ) use ($state_ids, $country_ids) {
// insert the whereOr queries here against the $q1 using the data in `use` params
})
->get();
To check your query there is a toSql() function you can use in laravel too.

Laravel Eloquent where and or between in one query

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

How to limit results of a left-join with Doctrine (DQL)

In a symfony 2 application, I have 2 entities mapped by a oneToMany relation (user and rendezvous).
I'm trying to search into my user entity and join the last rendezvous for each user found.
The idea is something like that :
$qb = $this->createQueryBuilder('p');
$qb->select('p.id, p.last_name, p.first_name')
->leftJoin('p.rendezvous', 'i')
->select('i.date')
->where('i.user = p.user')
->orderBy('i.date', 'DESC')
->setFirstResult(0)
->setMaxResults(1)
->where('p.user IN ('.$users.')')
->orderBy('p.last_name', 'ASC')
->addOrderBy('p.first_name', 'ASC');
I should have results like :
1, Ben, Tooch, 2014-10-15 18:45:00
7, John, Snow, 2014-10-16 17:15:00
...
I tried to use the paginator function but without any success.
Thank you very much for your help.
As I add some more columns to get, I had to find another way to do it. I finally got a working DQL query :
$qb->select('p.id, p.last_name, p.first_name, r.date, r.othercolumn')
->leftJoin('p.rendezvous', 'r', 'WITH', 'p.id = r.user AND r.date = (SELECT MAX(r2.date) FROM \App\YourBundle\Entity\Rendezvous as r2 WHERE r2.user = p.id)')
->where('p.user IN ('.$users.')')
->orderBy('p.last_name', 'ASC')
->addOrderBy('p.first_name', 'ASC')
->groupBy('p.id');
try groupBy the entity you want to setMaxResults on.
Have you tried outputing the generated SQL and running it?
I'm not sure if you can use maxresults and firstResult in a left join like this, but if your idea is just to recover users and their last rendezvous, you could use max(i.date) and group by p.id, p.last_name, p.first_name.
But if you want to page it, simply join the two tables and order by i.date.
Hope it helps!

codeigniter join not working

<?php
$this->db->select('*');
$this->db->from('venue');
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
$this->db->join('venue_type vt2', 'vt2.venue_type_id = venue.venue_type_id2');
$this->db->join('venue_subtype vst2', 'vst2.venue_subtype_id = venue.venue_subtype_id2');
$this->db->join('venue_type vt3', 'vt3.venue_type_id = venue.venue_type_id3');
$this->db->join('venue_subtype vst3', 'vst3.venue_subtype_id = venue.venue_subtype_id3');
$this->db->where('venue_id',$id);
$query = $this->db->get();
i have venue table it has more then 1 field relation b/w venue_type. When i try to give first relation
<?php
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
its working fine , but i try to access whole its not working.
Please Help me. (It may simple but i stuck)
By Saravanan.
You need to use alias for multiple joins.
SELECT st_id, table1.us_login, table2.us_login
FROM (stream)
LEFT JOIN users AS table1 ON table1.us_id = stream.st_id_user_from
LEFT JOIN users AS table2 ON table2.us_id = stream.st_id_user_to
see the link: http://codeigniter.com/forums/viewthread/151176/
There's no $this->db->from function in Codeigniter. Use instead $this->db->select('venue.*')

Resources