Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel modify Auth::user() query?

I want to add some joins onto my Auth::user() query. How do I do this without creating a brand new query? I just want to be able to make the default call of Auth::user() different than:

    SELECT * FROM `users` WHERE `id` = ?

to

    SELECT * FROM users INNER JOIN user_icons ON user_icons.ID = users.iconid WHERE `id` = ? 

I'm using the default model User class.

like image 603
Patchesoft Avatar asked Jan 13 '15 16:01

Patchesoft


People also ask

What is auth ()- user () in Laravel?

Auth::user() — You can check if a user is authenticated or not via this method from the Auth Facade. It returns true if a user is logged-in and false if a user is not. Check here for more about how Facades work in Laravel.

What is Auth :: Routes () in Laravel?

Auth::routes() is just a helper class that helps you generate all the routes required for user authentication. You can browse the code here https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/Router.php instead.

What does Auth :: check do in Laravel?

In other words, Auth::check() calls Auth::user() , gets the result from it, and then checks to see if the user exists. The main difference is that it checks if the user is null for you so that you get a boolean value. As you can see, it calls the user() method, checks if it's null, and then returns a boolean value.


2 Answers

Laravel provides a way for you to extend the Auth functionality. First, you need to create a class that implements the Illuminate\Auth\UserProviderInterface. Once you have your class, you call Auth::extend() to configure Auth with your new class.

For your case, the easiest thing for you to do would be to create a class that extends Illuminate\Auth\EloquentUserProvider. You'll want to update the retrieveBy* methods to add in your custom joins. For example:

class MyEloquentUserProvider extends Illuminate\Auth\EloquentUserProvider {
    public function retrieveById($identifier) {
        return $this->createModel()->newQuery()->join(/*join params here*/)->find($identifier);
    }
    public function retrieveByToken($identifier, $token) {
        // your code with join added here
    }
    public function retrieveByCredentials(array $credentials)
        // your code with join added here
    }
}

Once your class is fleshed out, you need to tell Auth to use it:

Auth::extend('eloquent', function($app) {
    return new MyEloquentUserProvider($app['hash'], $app['config']['auth.model']);
});

The first parameter to the Auth::extend method is the name of the auth driver being used as defined in app/config/auth.php. If you want, you can create a new driver (e.g. 'myeloquent'), but you'd need to update your Auth::extend statement and your app/config/auth.php driver.

Once all this is done, Auth::user() will end up calling your MyEloquentUserProvider::retrieveById method.

Fair warning: I have not actually done this myself, and none of this is personally tested. You will probably want to check out the documentation (L4.1 docs, L4.2 docs) and look at the Laravel code.

Other notes:

  • People have already chimed in that this is probably not what you want to do. However, the this information may be helpful to you and others looking to extend Auth for some other reason.
  • Considering your inner join, if a user does not have an associated user_icons record, Auth::user() will not return a record anymore, and the user probably won't be able to log in at all.
like image 142
patricus Avatar answered Oct 13 '22 16:10

patricus


If you have 1:n relation:

Add a "icons" table to you database with a foreign key "user_id".

Add a "Icon" Model to your models.

<?php

class Icon extends Eloquent{

  ...

}
?>

In Model Class "User" add a function:

public function icons() {
    return $this->hasMany('Icon');
}

Now you can do this:

$userIcons = Auth::user()->icons();
like image 1
goldlife Avatar answered Oct 13 '22 17:10

goldlife