Call to undefined method App\Models\User::createToken()
the method createToken is in HasApiTokens trait, you should use it In your User Model
:
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
Laravel 6: Call to undefined method App\\User::createToken()
Since your guard is returning the wrong User
model, App\User
, you should check your auth configuration, 'config/auth.php'. In the providers
array adjust any provider, usually users
, that is using the App\User
model to App\Models\User
instead.
'providers' => [
'users' => [
'driver' => 'eloquent',
// 'model' => App\User::class,
'model' => App\Models\User::class,
],
...
],
Call to undefined method App\Models\StudentModel::createToken()
You need to use
the HasApiTokens trait that you imported on your User Model:
class StudentModel extends Authenticatable
{
use HasFactory, Notifiable, HasApiTokens;
//...
}
Laravel 8: undefined method 'createToken' intelephense(1013)
I encountered the same problem and solved by adding the line. For my case.
/** @var \App\Models\MyUserModel $user **/
$user = Auth::user();
I think the annotation line tell PHP intelephense that $user
variable is not Illuminate\Foundation\Auth\User
type but \App\Models\MyUserModel
type. Please try it.
Call to undefined method Laravel\Socialite\Two\User::createToken()
First of all the problem I was having with having a token generated by passport for users authentication after the first social login was because I was calling the createToken
method on the user returned by Socialite
. As explained by @JorisJ1 Socialite
does not have the createToken
function so my initial code threw an error.
Here's how I fixed it
public function handleProviderCallback($provider)
{
// retrieve social user info
$socialUser = Socialite::driver($provider)->stateless()->user();
// check if social user provider record is stored
$userSocialAccount = SocialAccount::where('provider_id', $socialUser->id)->where('provider_name', $provider)->first();
if ($userSocialAccount) {
// retrieve the user from users store
$user = User::find($userSocialAccount->user_id);
// assign access token to user
$token = $user->createToken('Pramopro')->accessToken;
// return access token & user data
return response()->json([
'token' => $token,
'user' => (new UserResource($user))
]);
} else {
...
}
}
Comments are welcomed if there is a better way for adding social authentication to API.
Call to undefined method Illuminate\Database\Query\Builder::withAccessToken()
i solved this by adding "use HasApiTokens, Notifiable;" in App/User.php
I get BadMethodCallException Call to undefined method App\Models\User::identifiableAttribute()
your have forgotten to use 'CrudTrait' in your User Model:
use Backpack\CRUD\app\Models\Traits\CrudTrait;
class User extends Authenticatable
{
use Notifiable,CrudTrait
.......
}
Related Topics
Php Fatal Error: Uncaught Pdoexception: Could Not Find Driver
Laravel - Form Input - Multiple Select for a One to Many Relationship
How to Update Stock Quantity Using MySQL in PHP
Laravel Eloquent Get Results Grouped by Days
How to Get Values of Multiple Selected (Dynamic) Checkbox in PHP
How to Use PHP'S Password_Hash to Hash and Verify Passwords
Should We Ever Check For MySQLi_Connect() Errors Manually
How to Squeeze Error Message Out of Pdo
Contact Form 7 Cause Http 500 Error
Failed to Open the System Default Web Browser. Is One Installed - Pgadmin4
How Remove the Backslash ("\") in the Json Response Using PHP
How to Delete Completed Orders in Woocommerce Using a My SQL Query
How to Get Filename from a Variable (Url) in PHP
Remove Useless Zero Digits from Decimals in PHP