Merim
Merim

Reputation: 1363

Laravel relationships confusion

I'm learning Laravel these days and want to create some tables, models, and stuff.

I have companies, jobs, and employees. If I understood correctly, public functions in model files are fields:

 public function jobs()
  {
    return $this->hasMany('App\Job');
  }

Does this mean that I can create another function for currentJob like this:

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

Also, do I need to add belongsTo for each relationship? For example, in the user model?

Upvotes: 0

Views: 517

Answers (2)

Mihir Bhende
Mihir Bhende

Reputation: 9055

For basic model relationships :

// Job Model

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

}

// Employee Model
public function jobs(){
   return $this->hasMany('App\Job');
}

If you want to get current job, you can add an appended property :

// Employee Model

protected $appends = ['current_job'];

//Then create function for  the same : 

public function getCurrentJob()
{
  return $this->jobs()->latest()->first();
}

Upvotes: 1

Abhay Maurya
Abhay Maurya

Reputation: 12277

Try to see it in English than in PHP, this way you will be able to use right functions in right places.

So you have 3 tables: companies, jobs, and employees

Now before you dig in with Model in Laravel, you need to know what are the relationships among those three tables.

I assume the relationship between companies and jobs is one to many which means one company can have many jobs.

The relationship between jobs and employees is one to one because one job can be assigned to one employee.

Now based upon your project, these relationships might be different but the first step is to establish the relationships among three tables.

Now, assuming that you do have the same relationships as I explained above, your Models will have the following "public" functions:

//Company Model

//this function will returns all the jobs associated with the specific company_id
public function jobs()
{
   return $this->hasMany('App\Job');
}

========

//Job Model

//this function will return the employee associated with the specific job_id
public function employee()
{
   return $this->hasOne('App\Employee');
}

//now you can also have a function to fetch the company to which the job "belongs to", this is a reverse case which means, the jobs table has "company_id"

public function company()
{
   return $this->belongsTo('App\Company');
}

And same you can do in employee model because each employee belongs to a job means has a job_id so you will use belongsTo relationship:

//Employee Model

    //now you can also have a function to fetch the job to which the employee "belongs to", this is a reverse case which means, the employees table has "job_id"

    public function job()
    {
       return $this->belongsTo('App\Job');
    }

One thing to note that hasOne and belongsTo are the functions which are the counterpart of each other.

So if Job model uses hasOne for Employee, Employee will use belongsTo for Job, considering the employees table has "job_id" as a foreign key. It just matters that which Model you are using, based upon that you can get details of another model using these functions.

For more details, of course, refer to official documentation. I hope it helps clear your confusion

Upvotes: 2

Related Questions