Simple laravel query - laravel-5

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.

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']);

laravel join gives wrong id

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

Collapse two collections to one in laravel

I need help with collapse my collections.
Its my code
public function showFriends(){
$request1 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',true]])
->orWhere([['user_id','=',Auth::user()->id],['accepted','= ',true]])
->get();
$request2 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where([['user_id','=',Auth::user()->id] , ['accepted','=',true]])
->get();
$all = collect($request1,$request2)->collapse();
return $all;
}
Problem is in $all variable because it return empty collection. Thank you for help.
Use the merge method on collections:
$all = $request1->merge($request2);
You can try this
$collection = collect($request1,$request2);
$collapsed = $collection->collapse();
You can also do this potentially with one query by utilizing union:
public function showFriends(){
$query1 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',true]])
->orWhere([['user_id','=',Auth::user()->id],['accepted','= ',true]]);
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where([['user_id','=',Auth::user()->id] , ['accepted','=',true]])
->union($query1)
->get();
return $request;
return $request->all(); // If you want an array rather than collection
}
The Laravel Documentation on unions:
The query builder also provides a quick way to "union" two queries
together. For example, you may create an initial query and use the
union method to union it with a second query

laravel join query is not working

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!
Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.
$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

Resources