skip to Main Content

I have two table: Clients, Employees

Clients structure:

id
company_name
address
email
phone
created_by_employee

Employees structure:

id
name
position
department_id

I want to get all clients who were added by employees of the same department as the logged user.

So if I am logged in, I would like to get a list of my clients (added by me) and clients added by other employees, but only from my department (department_id). I have a problem with constructing a query in Laravel using join.

How can I build a query?

2

Answers


  1. Department Model

    public function employees()
    {
        return $this->hasMany(Employee::class);
    }
    

    Employee Model

    public function department()
    {
        return $this->belongsTo(Department::class);
    }
    
    public function clients()
    {
        return $this->hasMany(Client::class, 'created_by_employee');
    }
    

    Client Model

    public function employee()
    {
        return $this->belongsTo(Employee::class,'created_by_employee');
    }
    

    Get clients who were added by an employee from the same department:

    $employee = AppModelsEmployee::find(1);
    $clients  = AppModelsClient::whereHas('employee', function ($query) use ($employee) {
        $query->where('department_id', $employee->department_id);
    })->get();
    
    return $clients;
    
    Login or Signup to reply.
  2. if you’re using different tables then do.

    Your table

    $myId = auth()->user()->id; //you will need created_by_employee
    
    $clients = Client::where('created_by_employee',$myID)->get();
    

    Others table

    Client Model

    public function created_by()
    {
        return $this->hasOne('App Employees', 'id', 'created_by_employee');
    }
    

    Not adding department_id on clients table might a mistake, unless if it’s not
    neccessary

    $myId = auth()->user()->id; //you will need created_by_employee
    $departmentID = auth()->user()->department_id; //you will need 
     //department_id
     
     //join tables
    
     $clients = Client::join('employees','employees.id','clients. 
     created_by_employee')->where('clients.created_by_employee','!=',$myID)- 
     >where('employees.id',$departmentID)- 
     >groupBy('clients.id')- 
     >select('clients.id','clients.company_name','clients. 
     created_by_employee','ect........')->with('created_by')->get();
    

    For same table

    $myId = auth()->user()->id; //you will need created_by_employee
    $departmentID = auth()->user()->department_id; //you will need 
     //department_id
     
     //join tables
    
     $clients = Client::join('employees','employees.id','clients. 
     created_by_employee')->where('employees.id',$departmentID)- 
     >groupBy('clients.id')- 
     >select('clients.id','clients.company_name','clients. 
     created_by_employee','ect........')->with('created_by')->get();
    

    You created_y to know who the client belongs to or who created the client

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search