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

ressil's avatar

Exclude or Hide from returned data

I am new to Laravel and I don't know should I do using eloquent. I have three tables employees, position and division. I'd like to hide employee or employees either division or position that assigned to them was soft deleted. Any suggestions? Can someone convert my query to eloquent.

Employee Model: public function position() { return $this->belongsTo(Position::class); }

public function division() 
{
    return $this->belongsTo(Division::class);
}

Here's my query: $employees = Employee::with('division', 'position', 'role')->where('status', '!=', 'Deleted')->orderBy('last_name', 'asc')->get();

SELECT first_name, last_name, title, p.deleted_at AS deleted_position FROM employees e LEFT JOIN employee_positions p ON e.position_id = p.id WHERE p.deleted_at IS NULL;

0 likes
8 replies
ressil's avatar

@vincent15000 hi, what i wanted is to hide or exclude the employee when the position is soft deleted. How to achieve it with this line.

$employees = Employee::with('division', 'position', 'role')->where('status', '!=', 'Deleted')->orderBy('last_name', 'asc')->get();

1 like
tykus's avatar

@vincent15000 just has is enough

$employees = Employee::query()
    ->has('position')
    ->with('division', 'position', 'role')
    ->where('status', '!=', 'Deleted')
    ->orderBy('last_name', 'asc')
    ->get();
2 likes

Please or to participate in this conversation.