php - Laravel how to retrieve specific value in array -


i have polymorphic relation between organizations , ratings. ratings table set so:

id - integer user_id - integer score - integer ratable_type - text ratable_id - integer 

calling $org->ratings retrieve array ratings associated specific org. however, want see if there rating specific user_id value.

basically, want retrieve rating user_id = auth::user()->id. possibly without looping through ratings see when $org->rating->user_id == auth::user()->id?

if don't need load relation, query db:

$org->ratings()->where('user_id', auth::id())->first(); // returns rating model or null  // in previous versions of framework (if auth has no id method) $org->ratings()->where('user_id', auth::user()->id)->first(); 

otherwise filter collection:

$org->ratings->first(function ($key, $rating) {      return $rating->user_id == auth::id(); }); // before: rating model or null 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -