Я сначала не хотел использовать модель User при аутентификаии, но в итоге она понадобилась, то ли это связано с Sanctum, я не помню. Точно то, что я ее использую при логировании: в таблице с историей по приложению автоматически кладется id пользователя, совершившего действие. Этот id берется из модели User, где фиксируется при аутентификации. Это позволяет не заморачиваться с хранением логина в поле с JSON со всеми изменениями, что удобно

This commit is contained in:
vasya
2026-03-15 17:21:21 +03:00
parent b734bdf849
commit ca157ace77
3 changed files with 40 additions and 20 deletions
+19 -17
View File
@@ -6,43 +6,45 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
* @var array<int, string>
*/
// protected $fillable = [
// 'name',
// 'email',
// 'password',
// ];
protected $fillable = [
'name',
'email',
'password',
'login',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
// protected $hidden = [
// 'password',
// 'remember_token',
// ];
protected $hidden = [];
/**
* Get the attributes that should be cast.
* The attributes that should be cast.
*
* @return array<string, string>
* @var array<string, string>
*/
protected function casts(): array
{
return [
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
+15
View File
@@ -0,0 +1,15 @@
-- custom.users definition
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`login` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Логин пользователя',
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
@@ -0,0 +1,3 @@
INSERT INTO custom.users (login,name,email,email_verified_at,password,remember_token,created_at,updated_at) VALUES
('dgavrilov',NULL,NULL,NULL,NULL,NULL,'2025-11-09 16:50:05','2025-11-09 16:50:05'),
('developer',NULL,NULL,NULL,NULL,NULL,'2025-11-20 13:45:05','2025-11-20 13:45:05');