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();
Related
I have a collection of user skills but i need is to get only skills as an array i tried this
$builder->where('job_id', $value)
->join('users', 'job_applicants.user_id', '=', 'users.id')
->join('user_skills', 'users.id', '=', 'user_skills.user_id', function ($join){
$join->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
})
->groupBy('job_applicants.job_id')
->get();
The exception message : Object of class Closure could not be converted to string
The join closure should be the first and only argument after the table name.
->join('user_skills', function ($join) {
$join->on('users.id', '=', 'user_skills.user_id')
->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
});
However, a selectRaw inside the join closure doesn't make a whole lot of sense. Logically, this isn't where you select data. You've joined the table, your select statement belongs in the main query, it doesn't belong contained within the join, like so:
$builder->selectRaw("job_applicants.*, GROUP_CONCAT(user_skills.skill, ', ')")
->where('job_id', $value)
->join('users', 'job_applicants.user_id', '=', 'users.id')
->join('user_skills', 'users.id', '=', 'user_skills.user_id')
->groupBy('job_applicants.job_id')
->get();
I have a 3 table questions,registrations,ssi_tracks i need to get details from the registraions table by corresponding to other two tables
i need to get details from registrations based on
questions.question_schedul=0 ,ssi_tracks.track_first_status
i have wrote query but it says the column is not found here is my query
$register = DB::table('registrations')
->join('questions', 'registrations.registration_id', '=', 'questions.question_id')
->join('ssi_tracks','registrations.registration_id','=','ssi_tracks.registration_id')
->select('address', 'model', 'chassis', 'delivery_date','ssi_tracks.track_first_status')
->where([["questions.question_schedul", "=", $dropselected] and ['ssi_tracks.track_first_status',0]])
->get();
Try this:
$register = DB::table('registrations as R')
->select('R.address', 'R.model', 'R.chassis', 'R.delivery_date','S.track_first_status')
->join('questions as Q', 'R.registration_id', '=', 'Q.question_id')
->join('ssi_tracks as S','R.registration_id','=','S.registration_id')
->where('Q.question_schedul', '=', $dropselected)
->where('S.track_first_status', '=', 0)
->get();
Make sure you have used the right column here from question table for matching registration id:
->join('questions as Q', 'R.registration_id', '=', 'Q.question_id')
try this query :
$register = DB::table('registrations')
->leftJoin('questions', 'registrations.registration_id', '=', 'questions.question_id')
->leftJoin('ssi_tracks','registrations.registration_id','=','ssi_tracks.registration_id')
->select('registrations.address', 'registrations.model', 'registrations.chassis', 'registrations.delivery_date','ssi_tracks.track_first_status')
->where(['questions.question_schedul'=>$dropselected,'ssi_tracks.track_first_status'=>0])
->get();
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();
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();
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();