laravel join gives wrong id - laravel

I have the following query:
\DB::table('Service_prices')
->join('Services', 'Services.id', 'Service_prices.service_id')
->join('Roles', 'Roles.id', 'Service_prices.id')
->join('Users', 'Users.role_id', 'Roles.id')
->where('Services.id', $service_id)
->select(['Service_prices.id as service_price_id', 'Roles.id as role_id', 'Service_id as service_id'])
->get();
the issue is that all tables has id and I need them all, but, I get same id for all, in other words it appears there is some conflict and one of the ids are overwriting the others.
how to solve this?

\DB::table('Service_prices')
->join('Services', 'Services.id', 'Service_prices.service_id')
->join('Roles', 'Roles.id', 'Service_prices.id')
->join('Users', 'Users.role_id', 'Roles.id')
->where('Services.id', $service_id)
->select('Service_prices.id as service_price_id', 'Roles.id as role_id', 'Services.id as service_id', 'users.id as user_id')
->get();

Related

Using Laravel Query Builder, how to join tables from two different databases

I have two different tables in sql server database on the same host that I need to join together in a query.
I would like to use the laravel QueryBuilder to do so.
I've tried so far:
return DB::table('users')
->select([
'users.id',
'Resources.FirstName'
])
->join('Resources.dbo.ID', 'Resources.UserID', '=', 'users.id');
It results in the following error: General error: 1 near ".": syntax error (SQL: select "users"."id", "Resources"."FirstName" from "users" inner join "Resources"."dbo"."ID" on "Resources"."ID" = "users"."id")
If I copy the query in my dabatase script editor and run it, it runs correctly and give the expected result.
I have also tried this
return DB::table('users')
->select([
'users.id',
'Resources.FirstName'
])
->join(DB::raw('Resources.dbo.ID'), 'Resources.UserID', '=', 'users.id');
return DB::table('users')
->select([
'users.id',
'Resources.FirstName'
])
->join(DB::raw('Resources.ID'), 'Resources.UserID', '=', 'users.id');
->join('Resources', function($q) {
$q->connection('tcollect')->on('Resources.ID', '=', 'users.id');
});
Is there a way to achieve this?
Please try that
DB::table('Database1.Resources as dt1')-> join('Database2.users as dt2', 'dt2.id', '=', 'dt1.UserID')->select(['dt1.*','dt2.*'])->get();

Laravel 8 - How to write statement with paginate instead with get

I use Alias name of column because I have two columns in different tables with the same name, when retrieve them one column will override another column
DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->get(['*','trips.name As tirp_name','trip_user.id As reservation_id'])
How can I write it with paginate?
Try it this way:
$result = DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->paginate(100, ['*','trips.name As tirp_name','trip_user.id As reservation_id']);

how laravel join three tables with advance join clause using variable

Larvel have a question about inner joins with multiple tables and on values. I did build my code like this in laravel.
$clients = clients::leftjoin('clients_payment_type','clients_topup', function($join) {
$join->on('clients_payment_type.user_id', '=', 'clients.id') AND
$join->on('clients_topup.user_id', '=', 'clients.id');
})->where('clients.reference_id','=',$reference_id)->get();
Try this :
$clients = clients::leftjoin('clients_payment_type','clients_topup')
->join('clients','clients_payment_type.user_id', '=', 'clients.id')
->join('client_toup','clients.id', '=', 'clients_topup.user_id')
->where('clients.reference_id','=',$reference_id)
->get();
Regards
Working Answer
$clients1 = DB::table('clients')
->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
->where('clients.reference_id','=',$reference_id)
->get();

Sql error in laravel 5.4

Here I joined 2 tables addprojects and users. But It show me error. How can I fix it?
$test= addproject::select('addprojects.id','addprojects.proj_name',DB::raw("DATEDIFF(addprojects.proj_starts,addprojects.proj_end)AS Days"))
->join('user', 'users.emp_id', '=', 'addprojects.emp_id')
->where('id', 1)->get();
what the error appeared to you ?
check this example below, i had done it to join tables :
$nextvisits = patients::join('visits', 'visits.patient_id', '=', 'patients.id')
->join('users', 'users.id', '=', 'visits.user_id')
->where('visits.revisit_date','>=',$now_str)
->where('patients.clinic_id', $user->clinic_id)
->where('visits.clinic_id', $user->clinic_id)
->where('visits.active','=','1')
->get();

Simple laravel query

A simple query to get some additional information to pass to a view, produces an unnecessarily complex result. The result is an array containing an object which has the attribute I'm interested in.
Isn't there a simple way to just get the attribute?
$TheDriver = DB::table('rentals')
->join('users', 'users.id', '=', 'rentals.renter_id')
->select('users.name')
->where('users.id', '=', $Car->Driver)
->distinct()
->get();
and var_dump($TheDriver); produces this
array(1) { [0]=> object(stdClass)#236 (1) { ["name"]=> string(6) "Mr Tourist" } }
I'd like to simply get the result "Mr Tourist"
You can use the pluck() method to get only certain columns.
$TheDriver = DB::table('rentals')
->join('users', 'users.id', '=', 'rentals.renter_id')
->select('users.name')
->where('users.id', '=', $Car->Driver)
->distinct()
->pluck('name');
Try this one if you are get only one record.
$TheDriver = DB::table('rentals')
->join('users', 'users.id', '=', 'rentals.renter_id')
->select('users.name')
->where('users.id', '=', $Car->Driver)
->distinct()
->first();
get single object above query.

Resources