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 :
TheSeries
jobs.TheMovies
jobs.TheSeries
or TheMovies
data.I need a simple Eloquent query which will select all TheSeries
which have at least one TheJobs
.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With