Reputation: 3
I'm new to this Laravel Eloquent game and am getting a little stuck trying to create a query with dynamic variables set from another database.
I have one table named articles, where users can store articles and information about the articles. Users can group articles in magazines (table: magazine), each which can have multiple articles and which is associated with a user. Each article can be associated with multiple magazines
I'm trying to come up with a query that pulls the article_id of all the articles in a specific magazine and then uses the array of article_id to get an array of all information from articles that correspond to those article_id.
Help?!
EDIT 1: Thanks for the feedback for a newbie. If you believe this has been answered before, I would appreciate some guidance into the right terms to search for.
The query that I tried to use was:
$mygazineQuery = (new UserMygazineArticles())->where('mygazine_id', $mygazineSearch);
foreach ($MygazineQuery AS $key => $val) {
$MygazineQuery[$key] = (array)$val;
}
$codes = array_column($MygazineQuery, 'article_id');
$articles = Articles::whereIn('id', $codes)->get();
**Table UserMygazineArticles**
`id` int(11) NOT NULL AUTO_INCREMENT,
`articles_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`mygazine_id` text NOT NULL,
`saved` datetime NOT NULL,
PRIMARY KEY (`id`)
Upvotes: 0
Views: 48
Reputation: 2793
With Eloquent you can join tables together. This is untested and based on assumptions how your tables look like in detail:
$query = \DB::table('articles')->select("articles.*");
$query->join('user_articles_magazine', 'articles.id', '=', 'user_articles_magazine.articles_id');
$query->join('magazine', 'magazine.id', '=', 'user_articles_magazine.magazine_id');
$query->where('magazine.id', $magazineId);
return $query->get();
Upvotes: 0