Пытаюсь сгенерировать токен с помощью метода createToken, но говорит, что такого метода нет. Я делал все как в документации: 1. Сначала добавил в User
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
}
2. И в контреллере генерировал токен
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Http\Requests\LoginUserRequest;
use App\Http\Requests\StoreUserRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
/**
* Display a listing of the resource.
*/
public function register(StoreUserRequest $request)
{
return User::create($request->all());
}
/**
* Store a newly created resource in storage.
*/
public function login(LoginUserRequest $request)
{
if(!Auth::attempt($request->only(['email', 'password']))) {
return response()->json([
"success" => false,
"message" => "Login failed"
], 401);
}
$user = User::query()->where('email', $request->email)->first();
return response()->json([
"success"=> true,
"message"=> "Success",
'token' => $user->createToken("Token of user: {$user->name}")->plainTextToken
]);
}
public function logout()
{
return 'reg';
}
}
И вот моя модель юзеров
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
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 HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}