I am running Lumen (5.6.3) (Laravel Components 5.6.*). I'm trying to build a Rest api using this tutorial(https://www.youtube.com/watch?v=eWoJ2YbdrWU&t=5s). Here is my games.php code:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Bican\Roles\Traits\HasRoleAndPermission;
use Bican\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract;
use Illuminate\Database\Eloquent\Model;
class games extends Model implements AuthenticatableContract, AuthenticatableContract, HasRoleAndPermissionContract
{
use HasApiTokens, Authenticatable, Authorizable;
protected $table="games";
protected $fillable = ['Team 1','Team 2','Score 1','Score 2','Game Date','Viewers'];
}
The error I get after running is:
PHP Fatal error: Trait 'App\HasApiTokens' not found in C:\Users...lumen-api\app\games.php on line 14
Fatal error: Trait 'App\HasApiTokens' not found in C:\Users\...lumen-api\app\games.php on line 14
In games.php line 14:
Trait 'App\HasApiTokens' not found
I did everything the same as the tutorial. I can't solve this error in part 2.
Do you have composer? Install that first. Then you need Laravel Passport.
Run composer require laravel/passport
Run php artisan passport:install from command line
Add add the Laravel\Passport\HasApiTokens trait to your App\User model.
Try again.
You must include this "use Laravel\Passport\HasApiTokens;" at the top in your user model.
Add the Laravel\Passport\HasApiTokens trait to your App\User model. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user's token and scopes:
namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
}
Related
I followed the installation guide https://vanilo.io/docs/3.x/installation for vanilo step by step. Once all steps were completed, I tried to start up the development server with php artisan serve
and it threw an error:
InvalidArgumentException
Class App\Models\User must extend or implement Konekt\User\Contracts\User.
The default user model has been configured to extend the Konekt user model as described in the installation guide:
namespace App;
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 \Konekt\User\Models\User
{
The boot method in AppServiceProvider contains:
$this->app->concord->registerModel(\Konekt\User\Contracts\User::class, \App\Models\User::class);
The error states that the user model needs to extend or implement a model from Contacts\User, and it does as declared in the Konekt\User\Models:
namespace Konekt\User\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Konekt\Enum\Eloquent\CastsEnums;
use Konekt\User\Contracts\Profile;
use Konekt\User\Contracts\User as UserContract;
use Konekt\User\Contracts\Profile as ProfileContract;
use Konekt\User\Events\UserWasActivated;
use Konekt\User\Events\UserWasCreated;
use Konekt\User\Events\UserWasDeleted;
use Konekt\User\Events\UserWasInactivated;
/**
* User Entity class
*
*/
class User extends Authenticatable implements UserContract
{
I've followed this installation guide twice over from scratch and it always leads to this error. I need help troubleshooting, as it appears everything is done properly.
Fixed by changing the namespace on the User model to default, despite the tutorial showing namespace App, I used namespace App\Models and that fixed it.
I am using laravel spatie package for role permission, but i have got this error, here is my code
$user = User::find(1);
$user->givePermissionTo('manager_product_create');
You probably forgot to add the trait to the User class:
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// ...
}
see https://spatie.be/docs/laravel-permission/v3/basic-usage/basic-usage
I have installed the following package into my application so that users are able to follow and unfollow other users.
https://github.com/hypefactors/laravel-follow
In the documentation it states:
Preparing the Eloquent Models To allow an entity to be followed or to
follow other entities, the corresponding models have to implement an
interface and make usage of a trait.
Here's how we do it for a User and Company entity, where a user will
be able to follow a company and the company will be able to be
followed:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Hypefactors\Laravel\Follow\CanFollow;
use Hypefactors\Laravel\Follow\Contracts\CanFollowContract;
class User extends Model implements CanFollowContract
{
use CanFollow;
}
However my user model looks like this:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
I have tried including the files in various ways with no success. I assume its something to do with my user model class looking like this:
class User extends Authenticatable
and not this:
class User extends Model
Can anyone point me in the right direction?
If i do this:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Hypefactors\Laravel\Follow\CanFollow;
use Hypefactors\Laravel\Follow\Contracts\CanFollowContract;
class User extends Authenticatable implements CanFollowContract
{
use Notifiable;
use Uuids;
use CanFollow;
I get the following error:
Trait 'Hypefactors\Laravel\Follow\CanFollow' not found
I decided to change package to this: https://github.com/overtrue/laravel-follow which has worked nicely as expected.
#ceejayoz Thanks for your suggestions
I want to use "Jenssegers\Mongodb" package in a Laravel 5.5 app.
The package documentation tells to use:
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model {}
instead of:
use Illuminate\Database\Eloquent\Model;
class User extends Model {}
But, inUser model:
class User extends Authenticatable{}
and in "Illuminate\Foundation\Auth\User" class (what 'Authenticatable' refers to) it's using Illuminate\Database\Eloquent\Model not Jenssegers\Mongodb\Eloquent\Model.
I found this solution to extend what 'Authenticatable' extends directly in my model instead of extending 'Authenticatable' itself, so that I can use Jenssegers\Mongodb\Eloquent\Model;
Is there another better solution or should I make it like that??
use Jenssegers\Mongodb\Auth\User as Authenticatable;
then
class User extends Authenticatable
<?php
namespace App;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface
{
}
Your model should extend the Model class:
use Illuminate\Database\Eloquent\Model;
class User extends Model
If you want to use Laravel authentication system, it should extend Illuminate\Foundation\Auth\User:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable