User model getIsAdminAttribute()

https://www.youtube.com/watch?v=j97iBwTPlNE

https://stackoverflow.com/questions/58889575/laravel-5-8-authuser-is-not-using-user-model

  
public function getIsAdminAttribute()  
{  
   return (bool) $this->admin;  
}  
  
Auth::user()->isAdmin or auth()->user()->isAdmin  

https://laracasts.com/discuss/channels/code-review/how-to-check-user-role-after-login

  
namespace App;  
  
use App\Role;  
use Illuminate\Database\Eloquent\Model;  
  
class User extends Model  
{  
    public function getIsAdminAttribute()  
    {  
        return $this->roles->pluck( 'name' )->contains( 'admin' );  
    }  
  
    public function roles()  
    {  
        // you will need a role model  
        // Role::class is equivalent to string 'App\Role'  
        return $this->belongsToMany( Role::class, 'users_role' );  
    }  
}  

```LoginController```
  
namespace App\Http\Controllers;  
  
use Illuminate\Http\Request;  
use Auth;  
use Input;  
use Validator;  
use Redirect;  
  
class LoginController extends Controller  
{  
    //  
      
    public function login_post( Request $request )  
    {  
        $data = Input::except( array( '_token' ) );  
  
        // var_dump($data);  
  
        $rule = array(  
            'email'    => 'required|email',  
            'password' => 'required',  
        );  
  
        $validator = Validator::make( $data, $rule );  
  
        if ($validator->fails()) {  
            // should do something  
        } else {  
            // no need to populate $data again with the same values  
            // $data = Input::except( array( '_token' ) );  
              
            if (Auth::attempt( $data )) {  
                // here i want to check logged in user role  
                $user = Auth::user();  
                  
                if ($user->roles->pluck( 'name' )->contains( 'admin' )) {  
                    return Redirect::to( '/admin-dashboard' );  
                }  
                  
                return Redirect::to( '/dashboard' );  
            }  
        }  
    }  
}  

https://laravel.tw/docs/5.2/eloquent-serialization#appending-values-to-json

https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/111359/

方法簽名為getXXXAttribute
get XXX Attribute So XXX => IsAdmin``` public function getIsAdminAttribute()
{
return $this->roles()->where(‘id’, 1)->exists();
}


https://stackoverflow.com/questions/44017571/pass-current-eloquent-into-getxattribute-in-model

public function getIsAdminAttribute()
{
return Admin::where(‘user_id’, $this->id)->count() > 0;
}