Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afoysal's avatar

Use find() and with() together in Laravel query

I have 2 tables employees and employee_locations. One employee has many locations. I need to find out one employee record with related latest employee_locations record. I wrote below query.

$employees = Employee::find([1])->with('employees.employee_locations')->latest()->first();

I am getting below error

BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::with does not exist.
0 likes
10 replies
Sinnbeck's avatar

With goes before find()

$employees = Employee::with('employees.employee_locations')->find([1]);

Or if you need to alter the with query

$employees = Employee::with(['employees.employee_locations' => function($query) {
    $query->latest()->limit(1);
})->find([1]);
3 likes
mvd's avatar

Hi @afoysal

Edit: @sinnbeck was faster :D

Try this

Employee::with('employees.employee_locations')->find(1);
2 likes
afoysal's avatar

I am getting Call to undefined relationship [employees] on model [App\Employee]. error.

Sinnbeck's avatar

Well the error tells you whats wrong. You dont have a relationship called employees. Can you show the relationship you are trying to use?

1 like
afoysal's avatar

@sinnbeck

Here is Employee Model

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;
   use App\Employee_location;

    class Employee extends Model
   {
        protected $fillable = ['name', 'emi_number', 'account_id'];

       public function employee_locations()
        {
           return $this->hasMany(Employee_location::class);
       }
   }
Nakov's avatar

@afoysal you don't have to specify the current table in with, so just this should work:

$employee = Employee::with('employee_locations')->find([1]);
1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The problem is that is is just employee_locations not employees.employee_locations

$employees = Employee::with('employee_locations')->find([1]);
or
$employees = Employee::with(['employee_locations' => function($query) {
    $query->latest()->limit(1);
})->find([1]);
2 likes
afoysal's avatar

Thanks @sinnbeck your solution is working. Could you please explain when should I use => function($query) {..... ? Thanks

Sinnbeck's avatar

Sure..

Lets say you just want to get all data that the default query would get, then you just do this

$employees = Employee::with('employee_locations')->find([1]);

But now lets say that you want to change some part of relation.. That could be sorting, amount or something else. Then you would use a function to specify that. Notice how the function is linked to the 'employee_locations'

$employees = Employee::with(['employee_locations' => function($query) {
    $query->where('status', 1)->latest()->limit(5); //here we say that status of locations must be 1, that we want them order by latest and to only get 5.
})->find([1]);
2 likes

Please or to participate in this conversation.