php - Laravel Relationships vs. Runtime -> how to reduce amount of databse queries -
i love how laravel handels relationships lines of code. when there huge amount of data it's slowing down making many requests database.
class object extends model { public function users() { return $this->hasmany(user::class); } } class user extends model { proteced $appends = [ 'url' ]; public function object() { return $this->belongsto(object::class); } public function geturlattribute() { return 'exmaple.com/object/' . $this->object->value . '/user/' . $this->id; } }
when call
return object::with('users')->find(2);
it make new query database url generation.
so there clean way reduce amount of queries can speed application?
in case causing new queries because calling relationship isn't loaded on user. eager load more:
object::with('users.object')....
but keep going down line infinitely because of way these reference 1 another.
perhaps method doesn't belong on user model if intend on using way.
Comments
Post a Comment