Support the ongoing development of →
posted 6 years ago
Last updated 2 years ago.

You need to define the relationship:

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

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

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
Last updated 6 years ago.

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


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();


Sign in to participate in this thread!


Your banner here too?

dagord dagord Joined 6 Jun 2017


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

Your logo here?

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

© 2024 - All rights reserved.