Reputation: 155
laravel version : 5.0
Im currently making admin login middleware in laravel.
im using laravel default authentication out of its box. in this state, i have succesfully logged in.
as i know, i can get the user id by doing Auth::User()->id;
,
but the problem : i cant find way to retrive the table name;
below this is the result if i execute dd(Auth::User());
as you can see it cointains information #table:"admins"
Admin {#177 ▼
#table: "admins"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [▶]
#original: array:7 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
This is my middleware where i want to check the table name is admin or not :
<?php namespace App\Http\Middleware;
use Closure;
use Auth;
class AdminMiddleware extends Model{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::User()->getTable() == 'admins') //this doesnt work
{
return $next($request);
}
else
{
return response('Unauthorized.', 401);
}
}
}
im trying to look for it from laravel documentation and search in stackoverflow, seems like i get nothin.
Q : what method i need to call from Auth:: to obtain the table name?
if there is better practice dont hesitate to answer.
Upvotes: 2
Views: 2719
Reputation: 6361
Instead of creating two separate tables for admin and users put their credentials into the same table and use a second table namely roles which will contain details of the role of the users. If you want table structure and code for the same let me know and I will share it
Upvotes: 0
Reputation: 1406
If your Admin class
is extending the Eloquent\Model
, Admin::getTable()
or $adminInstance->getTable()
should work.
Upvotes: 4