[disclaimer: never use star * in a sql]..
select * from table1 inner join join_table on tabel1.id = join_table.id inner join table2 on tabel2.id = join_table.id
something like that I would have thought.
You said do it in ONE QUERY.. so i assumed you meant without eloquent I indeed got the wrong end of the stick with left/inner join as i thought you would want all matching rows..
Yes it sounded easy so I should have realized you meant in eloquent..
This is the thing I don't like about eloquent (or any other such tools), sometimes when you can do something with 1 query, it takes more than 1 query to do..
The code you're showing executes 2 queries.
And I wouldn't call that map
, instead I suggest this:
$photos = \Model\Photo::with(['tags' => function ($query) {
$query->select('tags.id')->where('tags.id', 12345);
}])
->get();
// Photo model
public function isTagged()
{
return (bool) count($this->tags);
}
Your approach adds tagged
attribute to post
model, unless you have public property/accessor called tagged
.
Cześć Jarek ;)
You are right, my solution produces 2 queries. I counted one more, but it was unrelated. Your solution doesn't help, because I don't want to know whether photo is tagged by any tag, but if it's tagged by one particular tag.
You didn't get my point.
It does exactly the same only a bit faster, but after you eager load the tags the same way you already did.
OK, but you can't add a method to the model assuming that somebody will do something before calling it. It's straight way to bugs that are hard to find.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community