0

i want to remove some post that have specific restrict value the posts are 2 type (artists and songs) , songs belong artists

i get the songs of artist by below code but when i use where function to remove some songs its not working ang got error

Error :

explode() expects parameter 2 to be string, object given

My Code

$rel = collect([]);
foreach ($post->artists as $artist) {
   $artist_posts = $artist->getPosts->where(function ($query) {
     $query->whereNotIn('restrict', ['res1'])->orWhereNull('restrict');                  
   })->sortbydesc('id');
   $rel = $rel->concat($artist_posts->where('type', $type));
}
$col = $rel->unique('id');

Post.php (model)

...
public function getPosts()
    {
        return $this->belongsToMany(Post::class, 'artists_posts', 'artist_id', 'post_id')->with('artists');
    }
 ...
2
  • what is $type? also why are you passing a closure to where ... that is Collection@where not a Query Builder where Commented Sep 22, 2020 at 12:52
  • I am assuming that relationship method you are showing is on Artist not Post? Commented Sep 22, 2020 at 13:02

1 Answer 1

1

You can't pass a Closure to the where method of Illuminate\Support\Collection. That is not a Query Builder you are calling where on, it is a Collection.

/**
 * Filter items by the given key value pair.
 *
 * @param  string  $key
 * @param  mixed  $operator
 * @param  mixed  $value
 * @return static
 */
public function where($key, $operator = null, $value = null)

If you wanted to call where on a Query Builder to do a database query you would have to use the actual relationship method not the dynamic property:

 $artist_posts = $artist->getPosts()->where(function ($query) {
                                  ^^

Then you would have to adjust sortbydesc to orderByDesc(...) and you would have to call get on the Builder to get the Collection of results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.