rossanmol
rossanmol

Reputation: 1653

Querying many-to-many relationship in Laravel while limiting relation existence

I have the following models in laravel:

TheJobs
- id
- description
- created_at
- deleted_at
- updated_at

TheSeries
- id
- title
- description
- created_at
- deleted_at
- updated_at


TheMovies
- id
- title
- description
- created_at
- deleted_at
- updated_at


mSeriesJobs
- seriesID
- jobID

mMoviesJobs
- movieID
- jobID

Here are the relationships for TheJobs:

 public function TheSeries() {
    return $this->belongsToMany('App\Models\TheSeries', 'mSeriesJobs', 'jobID', 'seriesID');
  }

  public function TheMovies() {
    return $this->belongsToMany('App\Models\TheMovies', 'mMoviesJobs', 'jobID', 'movieID');
  }

Here are the relationships for TheSeries:

  public function TheJobs() {
    return $this->belongsToMany('App\Models\TheJobs', 'mSeriesJobs', 'seriesID', 'jobID');
  }

same for Movies.

I would like to :

To clarify the question:

I need a simple Eloquent query which will select all TheSeries which have at least one TheJobs.

Upvotes: 6

Views: 282

Answers (3)

hktang
hktang

Reputation: 1833

From what I gathered, I believe you are looking for "Querying Relationship Existence", i.e. (wording from Laravel Doc) accessing the records for a model, while limiting the results based on the existence of a relationship.

In this case, if you need a simple Eloquent query which will select all TheSeries that have at least one TheJobs, I believe you can try has:

$TheSeries = App\TheSeries::has('TheJobs')->get();

The other way should work too, for jobs:

$TheJobs = App\TheJobs::has('TheSeries')->get();

Assumption: all model relations have been properly defined.

You can find more about "has" here: https://laravel.com/docs/5.4/eloquent-relationships (Search in-page for "Querying Relationship Existence" please.)

Upvotes: 1

Odin Thunder
Odin Thunder

Reputation: 3547

in Job model:

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

Series model:

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

get all TheSeries jobs:

$series = Series::whereHas('jobs')->get();
$seriesJob = $series->jobs;

Upvotes: 0

xhulio
xhulio

Reputation: 1103

there are 2 ways to do this. the first one is using eloquent relationships like you have started. in order to get all the jobs of a series all you have to do is:

$seriesCollection = TheSeries::all();
$seriesCollection->TheJobs();

The second method is better in my opinion. You will use accessors in order to retrieve what you want. So in the series model, to get all the jobs related to a series just do the following:

public function getSeriesJobsAttribute(){
    $job_series = DB::table('mSeriesJobs')->where('seriesID', $this->attributes['id'])->get(['jobID']);
    $job = TheJobs::whereIn('id', $job_series)->get();
    return $job;
}

and then in your view just iterate through $seriesCollection->series_jobs while in your controller you only retrieve the $seriesCollection;

Upvotes: 0

Related Questions