Мне нужно добавить пользовательский атрибут в модель User, когда пользователь входит в систему. Это значение отсутствует в базе данных, поэтому его необходимо добавить после.
Атрибут, который мне нужно добавить, называется client_id
Я везде ищу решение, но все, что я пробовал, не работает.Когда я выгружаю пользователя в другой контроллер, я не вижу добавленного мной атрибута.
Я удалил все, что пытался, и у меня осталась исходная модель.
Вот моя модель пользователя
<?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',
];
protected $appends = ['client_id'];
public function getClientIdAttribute()
{
// get the orgId from the session
$client_id = \Session::get('client_id');
return $client_id;
}
}
А вот мой 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');
}
}
Я связал добавление атрибута, используя $user->setAtttribute('client_id', 12345);
до \Auth::login($user);
, но он не работал