Reputation: 3534
I have a voters table and a voter_tags table:
voters:
id int
... // some other stuff
voter_tags:
id int
voter_id int
tag string
The voter_tag table is really simple and doesn't really need the id, but that's fine. I don't want to have a model for the voter_tag because that seems really heavyweight. (Is it?)
I want to add a function to the voter model:
class Voter extends Model {
public function voter_tag(){
return $this->hasMany(?); // what goes here?
}
// ... more stuff
}
While I know how to wire up that relationship with another model (eg the voter_phone model works fine), what if I just want to get a list of strings from that function call? Do I need to make a voter_tag model just to show a list of tags? It seems really heavyweight.
Is there a lightweight way for the ORM to include a list of strings from a join table?
Upvotes: 0
Views: 294
Reputation: 2556
Ok the eloquent way is not so heavyweight IMO. Is easy to setup and you get a lot of benefits and syntactic sugar. Under the hood is going to execute the exact same query. That being said. If you really don't want to create another model you can use the Query Builder like this:
DB::table('voter_tags')
->join('voters', 'voters.id', '=', 'voter_tags.voter_id')
->where('voters.id', '=', $this->id)
->count();
Hope it helps!
Upvotes: 1