How to make a DB query Laravel 5.6.?

Let's say I have a table "news" , the categories table and the intermediate table "Category"
How do I make a query to get all the news in one specific category.
June 3rd 19 at 18:54
2 answers
June 3rd 19 at 18:56
Set up connection and you do $category->news

Read more here
https://laravel.com/docs/5.6/eloquent-relationship...
I have obtained should be belongsToMany relationship ? - felicita.Nikolaus63 commented on June 3rd 19 at 18:59
that depends on whether one can be in multiple categories at once - freeda89 commented on June 3rd 19 at 19:02
Can.
I get this:
Model category -> belongsToMany relationship to the table "category"
Model news -> belongsToMany relationship to the table "Category"
Model Category-news -> link's belongsto to the News and Categories.
request do this $categoryNews = Category::find(1)->news;
news where the name of the method which defines the belongsToMany relationship to the table "category" - felicita.Nikolaus63 commented on June 3rd 19 at 19:05
no
Model Category -> hasMany to the news
Model news -> belongsToMany categories
And create a pivotal table category_news in which only 2 fields category_id, new_id (for it is not the right model)

Also prior to implementation to read it https://gist.github.com/greabock/3d1611c1125f5340f491 (just have the answers to your questions) - freeda89 commented on June 3rd 19 at 19:08
June 3rd 19 at 18:58
Table news like this. news(id, title, ...)
The Categories table categories(id, title)
Table category_news(id, category_id, news_id)

In the model Category write method news

public function news(){
 $news_ids = $this->hasMany("\App\CategoryNews", "category_id", id); // here ID get all the news from the table category_news
 $ids = "";
 foreach($news_ids as $item){
 $ids .= $item->news_id . ",";
}
 $ids = rtrim($ids, ",");
 $news = DB::select("SELECT * FROM news WHERE id IN($ids)");
 return $news;

}

something like that. Try it!
where should cause:

$category = Category::find(1);
$news = $category->news;
- felicita.Nikolaus63 commented on June 3rd 19 at 19:01

Find more questions by tags Laravel