2

I need to get all the news that has the same (one or more) categories as a specific new in an Eloquent query. And I dont know with pivot table.

I have 3 tables:

News

id | title | content

NewsxCategory (Pivot Table)

news_id | category_id

NewsCategory

id | name

Eloquent Models

// NewsCategory model
class NewsCategory extends Model
{

}

// News Model
class News extends Model
{

    public function categories()
    {
        return $this->belongsToMany(NewsCategory::class, 'news_x_category', 'news_id', 'category_id');
    }

}

I tried this.

In helper:

/**
 *  only related news
 *
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public static function relatedNews(News $new)
{
    $categories = $new->categories(); //obtain all categories of $new

    return News::whereHas('categories', function ($query) use ($categories) {
        $query->whereIn('new_id', $categories);
    });
}

And in view:

<div class="related-articles">
    <h5>{{ __('RELATED ARTICLES') }}</h5>
    <ul class="articles">
       @foreach ( App\Helpers\News::relatedNews($newsItem) as $new)
            <li>
                <h6>{{ $new->title }}</h6>
                <p>{{ $new->publication_date }}</p>
            </li>
       @endforeach
    </ul>
</div>

But helper always return null.

Also I tried in helper:

return News::with('categories')->where('category_id',$categories )->get();

But this option return all news.

I need all news related, I mean news with similar categories. Pivot tables give me a headache.

Thanks in advance!

1 Answer 1

2

In whereIn clause, you need to pass array of ids. But you are not passing correct.

So here is the correct one.

/**
 *  only related news
 *
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public static function relatedNews(News $new)
{
    $categoryIds = $new->categories->pluck('category_id')->toArray(); //obtain all categories of $new

    return News::whereHas('categories', function ($query) use ($categoryIds) {
        $query->whereIn('category_id', $categoryIds);
    });
}

I think after change above function changes. you will get related news.

Updated

If you want to print related news then use this:

@foreach ( App\Helpers\News::relatedNews($newsItem)->get() as $new)
    {{ $new->title }}
@endforeach
3
  • Thanks for you help but helper still return 0 news. I also tried: $categoryIds = $newsItem->categories()->pluck('category_id') and with this I obtain a collection with categoríes of this new. But result is the same... Jun 7, 2019 at 8:20
  • Can you try this ` $categoryIds = $new->categories->pluck('category_id')->toArray();`? Jun 7, 2019 at 8:21
  • This way If I use App\Helpers\News::relatedNews($newsItem)->count() obtain 3. That is correct. But when I try to access: @foreach ( App\Helpers\News::relatedNews($newsItem) as $new){ $new->title } I dont aobtain nothing. Why? Jun 7, 2019 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.