Support the ongoing development of Laravel.io →
posted 7 years ago
Eloquent
Last updated 2 years ago.
0

You need to define the relationship:

public function songs_number(){
  return $this->belongsTo('App\Songs');
}
Last updated 7 years ago.
0

I did but... this still doesn't work. I get the same error. How can I apply the method/filter from my route?

0
Route::get('/artist_list', function () {
    $artists = Artists::with(['songs_number'=>function($query){
      $query->where('songs_number.songs_number',100); // second songs_number should match your column field
    }])->get();
});
Last updated 7 years ago.
0

Yes, I already do this... but since my query is very complex (this was just an example), and I have to use it many times, I was wondering if it was possible to use a model method instead of repeating the same filter over and over

0

hi if you definition songs function in your artist model like this: public function songs() { return $this->hasMany('App\Song'); }

you can fetch artists that have exactly 100 songs with this query

Artists::has('songs', '=', 100)->get();

0

Sign in to participate in this thread!

Eventy

Your banner here too?

dagord dagord Joined 6 Jun 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.