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

phayes0289's avatar

Eloquent Query with Relationship is not working.

Earlier today, the AI response gave me what seemed to be a logical response to a big problem I was having. But I tried to implement the solution is recommended but am getting an error. It is almost as if it does not see the relationship.

So, I have the following query on a Training controller page that is generating an error.

$roster = User::with('currentAssignment')
        ->select(
            'users.id',
            'users.prefix',
            'users.firstname',
            'users.middlename',
            'users.lastname',
            'users.suffix',
            'users.status',
            'users.empnumber',
            'currentAssignment.rank',
            'currentAssignment.shift',
            'currentAssignment.station'
        )
        ->where('users.status', 1)
        ->orderBy('users.lastname', 'asc')
        ->orderBy('users.firstname', 'asc')
        ->orderBy('currentAssignment.division', 'asc')
        ->orderBy('currentAssignment.shift', 'asc')
        ->orderBy('currentAssignment.station', 'asc')
        ->get();

The error is this:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'currentAssignment.rank' in 'field list'

The relationship "currentAssignment is defined in the User model as this:

    public function currentAssignment()
    {
        return $this->hasOne(Assignment::class)
            ->where('effectivedt', '<=', now())
            ->orderBy('effectivedt', 'desc')
            ->orderBy('id', 'desc');
    }

What is wrong? It does not seem to recognize the relationship? Do I need to include a "use App\Models\Assignment" statement on the top of the model?

0 likes
3 replies
LaryAI's avatar
Level 58

The error message suggests that the column 'currentAssignment.rank' is not found in the field list. This error occurs because the 'currentAssignment' relationship is not being loaded correctly in the query.

To fix this issue, you need to make sure that the 'currentAssignment' relationship is properly defined and loaded in the query. Here's the solution:

  1. Make sure that the 'Assignment' model is imported at the top of the User model by adding the following line:
use App\Models\Assignment;
  1. In the User model, update the 'currentAssignment' relationship method to include the 'Assignment' model:
public function currentAssignment()
{
    return $this->hasOne(Assignment::class)
        ->where('effectivedt', '<=', now())
        ->orderBy('effectivedt', 'desc')
        ->orderBy('id', 'desc');
}
  1. In the Training controller, make sure that the 'User' model is imported at the top of the file:
use App\Models\User;
  1. Finally, update the query to load the 'currentAssignment' relationship correctly:
$roster = User::with('currentAssignment')
    ->select(
        'users.id',
        'users.prefix',
        'users.firstname',
        'users.middlename',
        'users.lastname',
        'users.suffix',
        'users.status',
        'users.empnumber',
        'assignments.rank',
        'assignments.shift',
        'assignments.station'
    )
    ->join('assignments', 'users.id', '=', 'assignments.user_id')
    ->where('users.status', 1)
    ->orderBy('users.lastname', 'asc')
    ->orderBy('users.firstname', 'asc')
    ->orderBy('assignments.division', 'asc')
    ->orderBy('assignments.shift', 'asc')
    ->orderBy('assignments.station', 'asc')
    ->get();

Note that I've changed 'currentAssignment.rank' to 'assignments.rank' in the select statement, as the 'currentAssignment' relationship is loaded as 'assignments' due to the join.

This should resolve the issue and load the 'currentAssignment' relationship correctly in the query.

phayes0289's avatar

Can someone explain to me why I have to add a table join when I have defined a relationship? I thought that was the purpose. I am now very confused.

In addition, the code the AI recommended did not work. I hade to remove the "assignments." from the orderby cluse. Not sure why. So it now reads as

$roster = User::with('currentAssignment')
        ->select(
            'users.id',
            'users.prefix',
            'users.firstname',
            'users.middlename',
            'users.lastname',
            'users.suffix',
            'users.status',
            'users.empnumber',
            'assignments.rank',
            'assignments.shift',
            'assignments.station'
        )
        ->join('assignments', 'users.id', '=', 'assignments.user_id')
        ->where('users.status', 1)
        ->orderBy('users.lastname', 'asc')
        ->orderBy('users.firstname', 'asc')
        ->orderBy('division', 'asc')
        ->orderBy('shift', 'asc')
        ->orderBy('station', 'asc')
        ->get();
Snapey's avatar

you need a join if you want to sort all users by records in the assignments table

Please or to participate in this conversation.