Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

[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.

0
  1. Asterisk (or star as you called it) is no evil in SQL, I don't see why shouldn't anyone use it.
  2. Your query doesn't have anything to do with my question, sorry. INNER JOIN will skip records without match and I said I need them all. LEFT JOIN is for that. But even then, how does is answer my question?
  3. I know how to do that with pure SQL, I'm asking how to do it Eloquent way.
Last updated 9 years ago.
0

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..

0

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.

0

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.

Last updated 9 years ago.
0

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.

0

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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

kokokurak kokokurak Joined 21 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.