Viewed   82 times

Sometimes, we'd like to separate users and admins in different 2 tables.
I think it is a good practice.

I am looking if that is possible in Laravel 5.



Before reading the following, you are supposed to have basic knowledge on ServiceProvider, Facade and IoC in Laravel 5. Here we go.

According to the doc of Laravel, you could find the Facade 'Auth' is refering to the IlluminateAuthAuthManager, which has a magic __call(). You could see the major function is not in AuthManager, but in IlluminateAuthGuard

Guard has a Provider. This provider has a $model property, according to which the EloquentUserProvider would create this model by "new $model". These are all we need to know. Here goes the code.

1.We need to create a AdminAuthServiceProvider.

public function register(){
    Auth::extend('adminEloquent', function($app){
        // you can use Config::get() to retrieve the model class name from config file
        $myProvider = new EloquentUserProvider($app['hash'], 'AppAdminModel') 
        return new Guard($myProvider, $app['']);
    $app->singleton('auth.driver_admin', function($app){
        return Auth::driver('adminEloquent');


class AdminAuth extends Facade {
        protected static function getFacadeAccessor() { return 'auth.driver_admin'; }

3. add the alias to Kernel:

'aliases' => [
    //has to be beneath the 'Auth' alias
    'AdminAuth' => 'AppFacadesAdminAuth'

Hope this could be helpful.

Monday, October 10, 2022

The var_dump() output is correct. $xml->StatusCode is a SimpleXMLElement instance. This is of course needed in case you have to, for example, add a child element to it:

$xml->StatusCode->addChild("test", "value");

If $xml->StatusCode contained only the value of the element rather than an instance of SimpleXMLElement, you wouldn't be able to do any modifications on the loaded XML.

So, what you need to do, is cast the value of StatusCode to a string. There are various ways of doing this:

var_dump($xml->StatusCode); // returns SimpleXMLElement instance
var_dump((string)$xml->StatusCode); // explicitly
var_dump($xml->StatusCode->__toString()); // explicitly, calling the magic function
echo $xml->StatusCode; // implicitly

Some demos

Wednesday, October 19, 2022

You'll want to do something like this:

SELECT grade
FROM Users
    SELECT id 
    FROM Users
    GROUP BY grade
    HAVING COUNT( grade ) = 1
) AND subject = 'english'`

Query builder has a ->whereIn that you should be able to use. Too lazy to test, but something like this may work.

$ids = user::select('id')
->having('gradecount', '=',1)

$grades = user::select('grade')
->whereIn('id', $ids)
->where('subject', 'english')
Wednesday, December 21, 2022

I don't think it's possible to use only function when you have code in your classes. Well, you could try with extending Blade but it's too much.

What you should do is creating one extra file, for example appHelpershelpers.php and in your composer.json file put:

"autoload": {
    "classmap": [
    "psr-4": {
        "App\": "app/"
    "files": ["app/Helpers/helpers.php"] // <- this line was added

create app/Helpers/helpers.php file and run

composer dump-autoload

Now in your app/Helpers/helpers.php file you could add those custom functions for example like this:

if (! function_exists('fooBar')) {
   function fooBar() 
      return AppHelpersCustomHelper::fooBar();

so you define global functions but in fact all of them might use specific public methods from some classes.

By the way this is exactly what Laravel does for its own helpers for example:

if (! function_exists('array_add')) {
    function array_add($array, $key, $value)
        return Arr::add($array, $key, $value);

as you see array_add is only shorter (or maybe less verbose) way of writing Arr::add

Friday, August 5, 2022

To archive this I created CustomMailServiceProvider by extending IlluminateMailMailServiceProvider so as to overwrite this method:

protected function registerSwiftTransport(){
    $this->app['swift.transport'] = $this->app->share(function($app)
    return new TransportManager($app);

Here is the complete solution

  1. I created CustomMailServiceProvider.php in appProviders

namespace AppProviders;

use IlluminateMailMailServiceProvider;
use AppCustomsCustomTransportManager;

class CustomMailServiceProvider extends MailServiceProvider{

    protected function registerSwiftTransport(){
        $this->app['swift.transport'] = $this->app->share(function($app)
            return new CustomTransportManager($app);
  1. I created CustomTransportManager.php in app/customs directory - NB: app/customs directory doesn't exist in default laravel 5 directory structure, I created it

namespace AppCustoms;

use IlluminateMailTransportManager;
use AppModelsSetting; //my models are located in appmodels

class CustomTransportManager extends TransportManager {

     * Create a new manager instance.
     * @param  IlluminateFoundationApplication  $app
     * @return void
    public function __construct($app)
        $this->app = $app;

        if( $settings = Setting::all() ){

            $this->app['config']['mail'] = [
                'driver'        => $settings->mail_driver,
                'host'          => $settings->mail_host,
                'port'          => $settings->mail_port,
                'from'          => [
                'address'   => $settings->mail_from_address,
                'name'      => $settings->mail_from_name
                'encryption'    => $settings->mail_encryption,
                'username'      => $settings->mail_username,
                'password'      => $settings->mail_password,
                'sendmail'      => $settings->mail_sendmail,
                'pretend'       => $settings->mail_pretend

  1. And finally, I replaced 'IlluminateMailMailServiceProvider', in config/app.php with 'AppProvidersCustomMailServiceProvider',
Sunday, August 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :