2

HI This is my first project in Laravel 5.1. I am stuck in a laravel many to many relation please help I have a tables like

news

id | title | content

categories

id | title | cat_type

and the pivot table

category_news

id | category_id | news_id

and the models

 class News extends Model
{
   public function categories()
    {
        return $this->belongsToMany('App\Category');
    }
}

 class Category extends Model
{
   public function news() {

    return $this->belongsToMany('App\News');

  } 
}

how do i get all the news of cat_type=1 with it s related categories

please help

I tried

$news = News::whereHas('categories',
           function($query){    
              $query->where('cat_type','2');    
            })
            ->where('publish','1')
            ->orderBy('created_at', 'desc')
            ->take(5)
            ->get();//latest news

it gives a news but not related category please help thank you

sanu
  • 1,048
  • 3
  • 14
  • 28

2 Answers2

1

You could also use Eager Loading for your needs.

In this case, all you should do is to write something like the following:

$categoryAndNews = Category::with('news')->where('cat_type',2)->first();

You could also define constraints in the relationships context.

$categoryAndNews = Category::with(['news' => function($query){
    $query->orderBy('created_at', 'desc')
    ->take(5);
}])->get();

Hope it will be useful!

More details about the topic here: http://laravel.com/docs/5.1/eloquent-relationships#constraining-eager-loads

  • Can you be more specific? :) I don't have elements to help you otherwise... What error are you getting? The _orderBy_ is just skipped? The _take()_ method works? – Francesco Malatesta Sep 08 '15 at 08:24
  • yes actually what happen is 1) if a news belongs to multiple category it will show same news multiple time 2) order of news not functioning --thanks – sanu Sep 08 '15 at 17:32
  • 1
    Ok, I've done some searches... 1) Take a look to [this post](http://stackoverflow.com/questions/18403135/eloquent-orm-eager-loading-distinct-records-in-many-to-many-relationship). 2) Take a look to [this post](http://stackoverflow.com/questions/18861186/eloquent-eager-load-order-by/18882219#18882219). – Francesco Malatesta Sep 08 '15 at 17:39
0

You can query the Category first like

$category = Category::where('cat_type',2)->first();
$news = $category->news()->where->where('publish','1')
        ->orderBy('created_at', 'desc')
        ->take(5)
        ->get();
Basit
  • 936
  • 5
  • 13
  • Abdul Basit this will just give a news of one category – sanu Sep 08 '15 at 08:23
  • For multiple categories, you can replace first() by get() and add with('news') before it. $news = Category::with(['news' => function ($query) { $query->orderBy('created_at', 'desc'); }])->get(); – Basit Sep 08 '15 at 08:41
  • is this correct $category = Category::where('cat_type',2)->get(); $news = Category::with(['news' => function ($query) { $query->orderBy('created_at', 'desc'); }])->get(); – sanu Sep 08 '15 at 09:22