Laravel many-to-many relationship query with limited 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 ratios 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 ratios for TheSeries :

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

for movies.

I would like to:

  • Get all TheSeries .
  • Get all TheMovies assignments.
  • Get all jobs with TheSeries or TheMovies data.

To clarify the question:

I need a simple Eloquent query that selects all TheSeries that have at least one TheJobs .

+6
source share
3 answers

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

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

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

Another way should work for tasks too:

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

Assumption: all model relationships were correctly defined.

You can find more information about "has" here: https://laravel.com/docs/5.4/eloquent-relationships (Search the page for "Request for the existence of existence", please.)

+1
source

in the task model:

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

Series Model:

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

Get all TheSeries quests:

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

There are two ways to do this. The first uses the eloquence of the relationship as you started. in order to get all the tasks of the series, you only need:

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

The second method is better in my opinion. You will use accessors to get what you want. Therefore, in the series model, to get all the tasks associated with the series, follow these steps:

 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 opinion, just go through $seriesCollection->series_jobs , and in your controller you will only get $seriesCollection ;

0
source

Source: https://habr.com/ru/post/1015118/


All Articles