I need to add a custom attribute to the User model when the user logs in. This value is not in the database so needs to be added after.
The attribute I need to add is called client_id
I've search everywhere for a solution but everything I've tried doesn't work. When I dump out the user in another controller, I don't see the attribute I added.
I've stripped out everything that I tried and I'm left with the original model.
Here is my User model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $connection = 'mysql';
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['username'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
];
}
And here is my LoginContorller
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use \DB;
// Models
use App\User;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request)
{
// Check validation
$this->validate($request, [
'username' => 'required|regex:/^([a-z0-9-_\.]+)*@([a-z0-9-]+)$/',
'password' => 'required'
]);
$user = User::where('username', $request->input('username'))
->where('isActive', 1)
->first();
// Set Auth Details
\Auth::login($user);
// Redirect home page
return redirect()->route('dashboard');
}
}
I tied adding an attribute using $user->setAtttribute('client_id', 12345); before \Auth::login($user); but it didn't work
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire