How do I use the Laravel ORM to join on a string column? -
i have voters table , voter_tags table:
voters: id int ... // other stuff voter_tags: id int voter_id int tag string
the voter_tag table simple , doesn't need id, that's fine. don't want have model voter_tag because seems heavyweight. (is it?)
i want add function voter model:
class voter extends model { public function voter_tag(){ return $this->hasmany(?); // goes here? } // ... more stuff }
while know how wire relationship model (eg voter_phone model works fine), if want list of strings function call? need make voter_tag model show list of tags? seems heavyweight.
is there lightweight way orm include list of strings join table?
ok eloquent way not heavyweight imo. easy setup , lot of benefits , syntactic sugar. under hood going execute exact same query. being said. if don't want create model can use query builder this:
db::table('voter_tags') ->join('voters', 'voters.id', '=', 'voter_tags.voter_id') ->where('voters.id', '=', $this->id) ->count();
hope helps!
Comments
Post a Comment