Tuesday 15 May 2012

Laravel 4.2 Eloquent query by relationship column value -



Laravel 4.2 Eloquent query by relationship column value -

good day all...

i'm trying access collection based on column in related table within eloquent (laravel 4.2).

i have next tables:

tags:

(int) id (string) name

tag_usage:

(int) id (string) model (the name of model allowed utilize tag)

tag_tag_usage: (pivot)

(int) id (int) tag_id (int) tag_usage_id

i have taggables (polymorphic store tags multiple models) table believe out of scope here want retrieve tags allowed utilize each model.

my tag model has relationship

public function usage() { homecoming $this->belongstomany('tagusage'); }

and tagusage model has

public function tags() { homecoming $this->belongstomany('tag'); }

now, want homecoming tags have specific usage, pseudo code be

get_tags->where(tag_usage.model = modelname)

which homecoming subset of tags.

tried few things no success on many fine brains available here.

many thanks.

you need utilize wherehas in next way:

$tags = tag::wherehas('usage', function($q) { $q->wheremodel('modelname'); })->get();

laravel laravel-4 eloquent

No comments:

Post a Comment