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

TheeShot's avatar

Eloquent Relationship advise

Hi,

I am a newby with Laravel, but loving it! So now i am exploring the possibilities and i came across an issue.

I want to try and build a time schedule submitter. For that I got 3 tables:

  • Clients
  • Employees
  • TimeSchedules

The timeschedule needs to be (eventually) a page where a Employee can write down his hours by day for a Client.

Table TimeSchedules id yearmonthday (as 20191015) Employee_id Client_id Hours

So a TimeSchedule can have multiple Clients, but only one Employee. (if i am correct)

I can't seem to get the relationships right.

Is there anyone who can advise me, how i should set up the relationships?

0 likes
4 replies
ftiersch's avatar

Your TimeSchedules would be a relationship between Clients and Employees with two additional informations (the day and the hours). So basically it's an m:n relationship which is a belongsToMany in Laravel with pivot data.

Think about it this way:

Every Employee can work for multiple clients. But every Client can also be "handled" by multiple Employees.

That's the definition of belongsToMany.

So you could define that relationship (example in your Employee model) like this:

public function clients() {
    return $this->belongsToMany(Client::class, 'TimeSchedules')
            ->withPivot(['yearmonthday', 'hours']);
}
Sinnbeck's avatar

In your TimeSchedule model this would be the employee.

public function employee()
    {
        return $this->hasOne('App\Employee');
    }

But I am wondering why a whole day is logged as only one entry? Dont you need to know at what times the employee worked for what client?

Edit: @ftiersch has a good solution :) Go with that

TheeShot's avatar

Thanks a lot Ftiersch!

I am a little confused though what to do with the TimeSchedulesController to get all info on the TimeSchedule page. I find it hard to wrap my head around the 3 (client, member, timeschedule) working together (maybe worth to notice: client and member do not have a Timeschedule_id in their table)

Client Class:

class Client extends Model
{
    protected $guarded = []; //$fillable[]

    public function employees() {
        return $this->belongsToMany(Employee::class, 'TimeSchedules')
                ->withPivot(['yearmonthday', 'hours']);
    }
}

Employee Class:

class Employee extends Model
{
    public function clients() {
        return $this->belongsToMany(Client::class, 'Timeschedules')
                ->withPivot(['yearmonthday', 'hours']);
    }
}

TimeScheduleController:

 public function show(Employee $employee)
    {
        return view('timeschedule', compact('employee'));
    }

Timeschedule.blade.php

<div>
        @foreach ($employee->clients as $client)
            <div>{{ $client->CompanyName }}</div>
            <div>{{ $client->Description}}</div>
            
        @endforeach
    </div>

This only results in 1 client, while the given EmployeeID has 2 clients. Also. How can I get the 'YearMonthDay' and 'hours' from the TimeSchedule on this page? Or am I trying to do something impossible?

I hope you are able to help me again :)

TheeShot's avatar

Thank you for your reply Sinnbeck. I don't need to know the times. I only want to know how many hours a day an employee worked for a client.

Please or to participate in this conversation.