First of all I should say I want to Use laravel ORM not query builder and not the Raw sql methods. so here is my question.
I have a model called bots that has belongTo(User::class) relation and 6 hasMany(TextPost::class) - hasMany(PhotoPost::class) and so on. I want to get bots that has at least one record in any of the hasMany(...) relations. so if a bot has a record in TextPost it should be returned and if a bot has no post in none of the hasMany(...) relations it should not be returned.
so far I have something like this in my User model
public function broadCasters()
{
return $this->bots->has('text_post')->orHas('photo_post')->orHas('media_post')->orHas('video_post')->orHas('audio_post')->orHas('document_post');
}
but its not going to work and says Call to a member function orHas() on boolean
so I wanted to change it to something like this:
public function broadCasters(){
return $this->bots->where(function($query){
return $query->has('TextPost')->orHas('PhotoPost') ...;
});
}
but this is not gonna work either and says Missing argument 2 for Illuminate\Support\Collection::where(). what should I do?
I use laravel 5.4. bu I saw this Question whereHas on multiple relationships - laravel 5.4 and its pretty close to what I want but no answers yet!!
also this question is another of my questions too, Combining AND/OR eloquent query in Laravel
but its not working either.
AND NOW THIS IS EXACTLY WHAT I WANT BUT NOT WORKING, I THINK BECAUSE ITS FOR LARAVEL 4 AND I USE LARAVEL 5.4
enter code hereIf you user
$this->bots, the result will be laravel collection array. You need to use$this->bots()like this :