Can Userprovider be optimized? #38231
-
We don't need to "select * from table_ name " every time, especially Auth:: id(), which will greatly affect the database performance. We can add an attribute to the model to get what attributes we need.Besides, can I use model association after Auth:: user()? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Currently there is no way. The developer expects a You "could" use a Global Scope on the User model, which would be replicated on the The only alternative is to extend the /**
* Get a new query builder for the model instance.
*
* @param \Illuminate\Database\Eloquent\Model|null $model
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function newModelQuery($model = null)
{
$query = is_null($model)
? $this->createModel()->newQuery()
: $model->newQuery();
return $query->select('name', 'email');
} |
Beta Was this translation helpful? Give feedback.
Currently there is no way. The developer expects a
first()
equivalent call when dealing with the authenticated user.You "could" use a Global Scope on the User model, which would be replicated on the
UserProvider
, but it would affect every query.The only alternative is to extend the
EloquentUserProvider
with your own version, and override thenewModelQuery()
method, where the query gets created from a model class name.