Using Laravel's WhereIn to search multiple tables - php

I have 3 SQL tables.
clients
events
client_events
Because a client can have multiple events, I made the third table to show those relationships. I am using the following code to retrieve all of the clients that have the have a record matching this event, but it is only returning 1 record when there are multiple.
$eventHosts = DB::table('clients')->whereIn('id', function($query) {
$query->select('client_id')->from('client_events')->where('event_id', '=', explode('/', $_SERVER['REQUEST_URI'])[2]);
})->get();
What am I overlooking?

You can fetch the ids first, then pass to the whereIn query.
$clientIds = DB::table('client_events')
->where('event_id', explode('/', $_SERVER['REQUEST_URI'])[2])
->pluck('client_id')
->toArray();
$eventHosts = DB::table('clients')->whereIn('id', $clientIds)->get();

To get the results in a single query and more efficeintly, try join
$eventHosts = DB::table('clients') //select your main table
->join('client_events','client_events.client_id','=','clients.id') //join it on related columns
->where('client_events.client_id',explode('/', $_SERVER['REQUEST_URI'])[2])) //apply your condition on client_events
->get();

Related

Laravel OrderBy by related table column

I have a table (A) that has a One to Many relation with another table (B).
I want to query Table A and eager load Table B with the Table A results - but I also want to sort Table A by a value in Table B.
I have tried using OrderBy in the query and also trying SortBy on the resultant collection but cannot get the Table A data to be sorted by the value found in Table B.
Example of what I have tried:
$query = ModelA::with("ModelB"])->get()->sortByDesc('ModelB.sortValue');
Keep in mind, I am only interested in the LATEST record from Table B. So I need to query Table A and sort by a value in the LATEST records of Table B.
How can I achieve this?
EDIT:
The below (as suggested by #ljubadr) works pretty close, but the issue is that there are many record in Table B which means that it doesn't reliably sort as it doesn't seem to sortby the latest records in Table B. Can I have the join return ONLY the latest record for each ID?
$query = ModelA::select('TableA.*')
->join('TableB', 'TableA.id', '=', 'TableB.col_id')
->groupBy('TableA.id')->orderBy('TableB.sortCol', 'desc')
->with(['x'])
->get();
EDIT 2:
#Neku80 answer has gotten me closest but it seems to not sort the column with the greatest accuracy.. I'm sorting a Decimal column and for the most part it is in order but in some places the items are out of order..
$latestTableB = ModelB::select(['TableA_id', 'sortByColumnName'], DB::raw('MAX(created_at) as created_at'))
->groupBy('TableA_id');
$query = ModelA::select('TableA.*')
->joinSub($latestTableB, 'latest_TableB', function ($join) {
$join->on('TableA.id', '=', 'latest_TableB.TableA_id');
})
->orderBy('latest_TableB.sortByColumnName')
->get();
For example, the ordering is like:
0.0437
0.0389
0.0247 <-- -1
0.025 <-- +1
0.0127
When I delete all rows except for the 'latest' rows, then it orders correctly, so it still must be ordering with old data...
I have found a solution:
ModelA::select('TableA.*', 'TableB.sortByCol as sortByCol')
->leftJoin('TableB', function ($query) {
$query->on('TableB.TableA_id', '=', 'TableA.id')
->whereRaw('TableB.id IN (select MAX(a2.id) from TableB as a2 join TableA as u2 on u2.id = a2.TableA_id group by u2.id)');
})
->orderBy('TableB.sortByCol')
->get();
Another alternative to order is like this:
$users = User::orderBy(
Company::select('name')
->whereColumn('companies.user_id', 'users.id'),
'asc'
)->get();
Here we are ordering in asc order by company name field.
In this article it is explained in detail.
You can simply execute a left join query:
ModelA::query()->leftJoin('model_b_table', 'model_a_table.primary_key', '=', 'model_b_table.foreign_key')->orderBy('model_a_table.target_column')->get();
This should work if you only need TableB's ID and created_at columns:
$latestTableB = ModelB::select('TableA_id', DB::raw('MAX(created_at) as created_at'))
->groupBy('TableA_id');
$query = ModelA::select('TableA.*')
->joinSub($latestTableB, 'latest_TableB', function ($join) {
$join->on('TableA.id', '=', 'latest_TableB.TableA_id');
})
->orderBy('latest_TableB.created_at')
->get();

Laravel query format - Join To the Latest row [duplicate]

This question already has answers here:
Laravel eloquent selecting most recent row from joined table
(3 answers)
Closed 3 years ago.
I have two tables for example users and orders.
I have to join users with orders On users.id = orders.user_id - which is quite fine.
User table has one to many relation in orders table.
I have some conditions also, like orders.pay_type = 'xyz', 'orders.date = yesterday' , 'user.status = active' ..etc.
My problem is that I need to join my user table to the latest row of orders table correspond to that user_id.
Or need to fetch the latest details of that users orders table details along with user table data.
I already tried
->orderBy('orders.date')
->groupBy('orders.user_id')
->get();
but it has no result in o/p.
$data= users::leftJoin('orders', function($join) {
$join->on('orders.user_id', '=', 'users.id')
->on('orders.id', '=', DB::raw("(SELECT max(id) from orders WHERE orders.user_id = users.id)"));
})
->select(*)
This resolve my issue.
If you don't want to change anything in model you can go with this method as well, which is simple query builder method.
DB::table('orders')->leftJoin('users', 'orders.user_id', 'users.id')
->groupBy('orders.user_id')
->orderBy('orders.date', 'DESC')
->get();
Assuming, You might have "orders" function in User.php model
You can also go for below method. (Similar to the answer already given but for array of users instead of one user.)
$users = User::all();
foreach ($users as $user) {
$latest_order = $user->orders()->where('pay_type', 'xyz')->orderBy('date', 'DESC')->first(); // Option 1
$latest_order = $user->orders()->where('pay_type', 'xyz')->latest('date')->first(); // Option 2
}
You can use the following Relation in the User Model
public function latest_order(){
return $this->hasOne('Order')->orderBy('orders.date', 'desc')->limit(1);
}
Update
Since you can not change the Model, you can use the following code to fetch the latest Order for each User.
$user = User::first(); //get first User
$user->orders()->latest('date')->first();

Joining multiple tables in laravel query builder results in duplication

I'm trying to use Laravel Query Builder to join 2 tables.
Here's my query for joining 2 tables
$instructors = DB::table('instructors')
->join('instructor_courses', 'instructors.id', '=','instructor_courses.instructor_id')
->where('instructor_courses.course_id', $schedule->course_id)
->where('instructor_courses.position', 'Instructor')
->whereNull('instructor_courses.deleted_at')
->whereNull('instructors.deleted_at')
->get();
The code let me join the table properly however this results to multiple duplication. Take a look at the result passed in select tag
Here's the schema I have
Instructor Table
Instructor Courses Table
Basically what I want to achieve is to display a record without duplication. Is there a better approach in joining multiple table like this one? Thank you
You should use groupBy for instructors.id
$instructors = DB::table('instructors')
->join('instructor_courses', 'instructors.id', '=','instructor_courses.instructor_id')
->where('instructor_courses.course_id', $schedule->course_id)
->where('instructor_courses.position', 'Instructor')
->whereNull('instructor_courses.deleted_at')
->whereNull('instructors.deleted_at')
->groupBy('instructors.id') //add a line
->get();
Try to use the ->distinct() function in laravel to prevent duplicates in selecting data.
$instructors = DB::table('instructors')
->join('instructor_courses', 'instructors.id', '=','instructor_courses.instructor_id')
->where('instructor_courses.course_id', $schedule->course_id)
->where('instructor_courses.position', 'Instructor')
->whereNull('instructor_courses.deleted_at')
->whereNull('instructors.deleted_at')
->groupBy('instructors.id')
->distinct()
->get();

How to determine columns that will be returned in the result when using `paginate()` method in Laravel?

I have 2 tables: users and articles. To fetch all columns from the articles table and only user_name column from the users table, I use this code:
$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
->get(array('articles.*', 'users.user_name'));
and it works fine, but when I use paginate() method like this:
$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
->paginate(10);
it fetches all columns from both tables, which I don't want. My question is: How can I select columns that will be returned in the result if I use paginate() method in Laravel framework?
The select function does this.
$articles = Article::join('users', 'articles.user_id', '=', users.user_id')
->select('articles.*', 'users.user_name')
->paginate(10);

How to make a join select in Laravel?

What I've got now is that I retrieve all the users and then loop each and check if there are an item, otherwise if there are no relation while we are retrieving the name of the item, we will get an error
$users = User::all();
foreach($users as $user)
{
if($user->item)
{
echo $user->item->name;
}
}
My question is how to select all the user records that have a relation in the items table? using the eloquent Model in laravel-4?
I'am asking this because here http://laravel.com/docs/eloquent - are no examples of using joins, only joining them after the selecting, but this is not good because we select all the rows instead of only those who has relation, also we will not have to check if the item exists, because we already know that the item really exists...
All you need is:
$users = User::has('item')->get();
1st example here http://laravel.com/docs/eloquent#querying-relations
http://laravel.com/docs/queries#joins
Try this
DB::table('users')
->join('item', 'users.id', '=', 'item.user_id')
->select('item.name')
->get();

Categories