Laravel Query Builder Nested Join and Alias - mysql

How to create this query using Laravel's Query Builder:
SELECT `content`.`tagable_id`,`taged`.`tag_id`
FROM `taged`
RIGHT JOIN
(SELECT `taged`.`tagable_id`,`taged`.`tag_id`
FROM `taged`
WHERE `taged`.`mask_flag`='0'
AND `taged`.`tagable_type`='App\\\Post'
AND `taged`.`user_id`='1') AS `content`
ON `taged`.`mask_flag`='1'
AND `content`.`tagable_id`=`taged`.`tagable_id`
AND `taged`.`tagable_type`='App\\\Post'
AND `taged`.`user_id`='1'
The parenthesis around the inner SELECT taged.tagable_id... is where my main problem lies.

Here is your Laravel query:
$result = DB::table('taged')
->select('content.tagable_id', 'taged.tag_id')
->rightJoin(DB::raw('SELECT tagable_id, tag_id FROM taged AS content'), function($join)
{
$join->on('taged.mask_flag', '=', 1);
$join->on('content.tagable_id', '=', 'taged.tagable_id');
$join->on('taged.tagable_type', '=', 'App\\\Post');
$join->on('taged.user_id', '=', 1);
})
->where('content.mask_flag', '=', 0);
->where('content.tagable_type', '=', 'App\\\Post');
->where('content.user_id', '=', 1);
->get();

Related

Laravel Joins with same table again and again

This is the current code of my joins.
$vehicleTransferNotifications=DB::table('vehicle_transfer_notifications')
->join('vehicle_requests', 'vehicle_transfer_notifications.request_id', '=', 'vehicle_requests.id')
// ->join('users', 'vehicle_requests.users_id', '=', 'users.id')
// ->join('companies', 'users.company_id', '=', 'companies.id')
// ->join('companies', 'vehicle_transfer_notifications.company_id', '=', 'companies.id')
->join('vehicles', 'vehicle_requests.vehicle_id', '=', 'vehicles.id')
->join('companies', 'vehicles.company_id', '=', 'companies.id')
->join('vehicle_types', 'vehicles.vehicle_type_id', '=', 'vehicle_types.id')
->join('vehicle_make', 'vehicles.make_id', '=', 'vehicle_make.id')
->select('vehicle_transfer_notifications.*','vehicle_transfer_notifications.id as notification_id','vehicle_transfer_notifications.seen as is_seen','vehicle_requests.*','vehicles.reg_no','vehicle_types.type','vehicle_make.name','companies.name as company_name')
->where('vehicle_transfer_notifications.company_id','=',auth()->user()->company_id)
->get();
Now I want add something like I have comment in my code.How I add to this join query? Because I have already join my query with companies table so how can I join again with the companies table and get something like companies.name again in the select query?
You should give alias to companies table as per your requirement.
use that alias for fetch data instead of companies
$vehicleTransferNotifications=DB::table('vehicle_transfer_notifications')
->join('vehicle_requests', 'vehicle_transfer_notifications.request_id', '=', 'vehicle_requests.id')
->join('vehicles', 'vehicle_requests.vehicle_id', '=', 'vehicles.id')
->join('companies as company_one', 'users.company_id', '=', 'company_one.id')
->join('companies as company_two', 'vehicle_transfer_notifications.company_id', '=', 'company_two.id')
->join('companies as company_three', 'vehicles.company_id', '=', 'company_three.id')
->join('vehicle_types', 'vehicles.vehicle_type_id', '=', 'vehicle_types.id')
->join('vehicle_make', 'vehicles.make_id', '=', 'vehicle_make.id')
->select('company_one.name as company_name_one', 'company_two.name as company_name_two', 'company_three.name as company_name_three', 'vehicle_transfer_notifications.*','vehicle_transfer_notifications.id as notification_id','vehicle_transfer_notifications.seen as is_seen','vehicle_requests.*','vehicles.reg_no','vehicle_types.type','vehicle_make.name')
->where('vehicle_transfer_notifications.company_id','=',auth()->user()->company_id)
->get();

Laravel 5.4 Error when Joining two tables

I need to join two tables in laravel model. Here is my query,
$id="1002";
$resultsInCalls = DB::table('qlog')
->select('user_master.fname','user_master.lname','qlog.data2')
->whereDate('created', '=', date('Y-m-d')) // this day
->join('qlog', 'qlog.agent', '=', 'user_master.sip_id')
->where('agent', '=', $id)
->where(function ($query) {
$query->where('event', '=', 'COMPLETEAGENT')
->orWhere('event', '=', 'COMPLETECALLER');
})
->take(5)
->get();
But I run this query I get following error.
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'qlog' (SQL: select `user_master`.`fname`, `user_master`.`lname`, `qlog`.`data2` from `qlog` inner join `qlog` on `qlog`.`agent` = `user_master`.`sip_id` where date(`created`) = 2017-11-29 and `agent` = 1002 and (`event` = COMPLETEAGENT or `event` = COMPLETECALLER) limit 5)
I read related questions. But I can't figure it out. How to fix this error.
Here I get it done,
$resultsInCalls = DB::table('qlog')
->join('user_master as user_table', 'qlog.agent', '=', 'user_table.sip_id')
->select('user_table.fname','user_table.lname','qlog.data2')
->whereDate('created', '=', date('Y-m-d')) // this day
->where('agent', '=', $id)
->where(function ($query) {
$query->where('event', '=', 'COMPLETEAGENT')
->orWhere('event', '=', 'COMPLETECALLER');
})
->take(5)
->get();

Join the same table with two different column laravel

I try these code in MySQl:
SELECT
A.*,
B.name,
C.name
FROM
eventlog_tbl as A
LEFT JOIN users B ON A.byuser=B.email
LEFT JOIN users C ON A.affectiveuser=C.email;
I try these in Laravel
return DB::table('eventlog_tbl')
->leftjoin('users', 'users.email', '=', 'eventlog_tbl.byuser')
->leftjoin('users', 'users.email', '=', 'eventlog_tbl.affectiveuser')
->select('eventlog_tbl.*','users.name','users.name')
->get();
How can i convert it to Laravel?
Try below code:
$res = DB::table('eventlog_tbl')
->leftjoin('users AS A', 'A.email', '=', 'eventlog_tbl.byuser')
->leftjoin('users AS B', 'B.email', '=', 'eventlog_tbl.affectiveuser')
->select('eventlog_tbl.*','A.name as byuser_name','B.name as affectiveuser_name')
->get();
print_r($res);
This is your query written using the Laravel query builder.
$events = DB::table('eventlog_tbl')
->select('eventlog_tbl.*', 'users_1.name', 'users_2.name')
->leftJoin('users AS users_1', 'users_1.email', '=', 'eventlog_tbl.byuser')
->leftJoin('users AS users_2', 'users_2.email', '=', 'eventlog_tbl.affectiveuser')
->get();
Edit:
$events = DB::table('eventlog_tbl')
->select('eventlog_tbl.*', 'users_1.name AS user_1', 'users_2.name AS user_2')
->leftJoin('users AS users_1', 'users_1.email', '=', 'eventlog_tbl.byuser')
->leftJoin('users AS users_2', 'users_2.email', '=', 'eventlog_tbl.affectiveuser')
->get();
The problem is that both name columns are called the same thing. As per the accepted answer, these will need to be aliased differently too.
Why don't you convert this to use relationships? I have probably got the relationships wrong, something like this:
class EventLog extends Model
{
public function byUser()
{
return $this->hasOne(User::class, 'byuser', 'id');
}
public function affectiveUser()
{
return $this->hasOne(User::class, 'affectiveuser', 'id');
}
}
And then
$event_log = EventLog::with(['byUser', 'affectiveUser')->all();
foreach ($event_log as $item) {
echo $item->byUser->email();
}

Where clause on join query laravel 4.2

I have 2 tables 'users' and 'instantUsers'. I want to join them on users.id = instantUsers.user_id and want to add 2 where clauses on the resulting. I'm not getting how to do both. The query I'm using is -
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join->on('users.id', '=', 'instantUsers.user_id');
})
->where('instantUsers.instantMode', '=', '1')
->where (function($query) use ($userId) {
$query->where('instantUsers.user_id', '!=', $userId);
})
->get();
You can try this one maybe this will help you:
DB::table('users as table1')->join('instantUsers as table2','table1.id','=','table2.fkId') ->where('table2.instantMode','=','1')->where('table2.user_id','!=',$userId)->get();
Your 'instantUsers.instantMode','=','1' expression can be done in a join, resulting in a better performance.
I would write it like this
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join
->on('users.id', '=', 'instantUsers.user_id')
->on('instantUsers.instantMode', '=', 1);
})
->where('users.id', '!=', $userId)
->get();

Join Table With Multiple Condition Laravel

i have the sql below:
$result = DB::table('tblA')
->join('tblB','tblB.id', '=', 'tblA.photoid')
->join('tblB','tblB.id', '=', 'tblA.linkedphotoid')
...
I have tblA with photoid and linkedphotoid and both link to one same table(tblB) and same column.
How to write it in laravel sql?
You can add OR condition in your join
$result = DB::table('tblA')
->join('tblB', function($join) {
$join->on('tblB.id', '=', 'tblA.photoid')->orOn('tblB.id', '=', 'tblA.linkedphotoid');
})
->get();