Call to undefined method App\User::givePermissionTo() - php

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

Related

Implement interface and trait into User model

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

User must implement CanResetPassword interface. Password Reset Laravel 5.4

password reset issue
User.php
namespace App;
use Eloquent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class User extends Eloquent implements Authenticatable
{
use AuthenticableTrait;
Add the interface CanResetPassword to your User class:
// at the top of the file
use Illuminate\Auth\Passwords\CanResetPassword;
// at the class declaration
class User extends Eloquent implements Authenticatable, CanResetPassword
{
// ...
}

trait 'App\HasApiTokens' not found in Lumen

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;
}

Class 'App\Eloquent' not found in laravel after running php artisan db:seed

<?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

Unable login user with Lumen and Laravel

First of all apologize for the basic question but I'm new in both Laravel and Lumen and trying to learn it.
I'm trying to login user using Lumen but I've got errors. What I'm trying to write is
if (auth()->attempt(...) { }
Tried also another approach like this
if (\Auth::attempt(...) { }
Both ways the error is
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given
I'm sure also that I validate all the inputs from the form.
It seems your User model extends Model instead of Authenticatable. Your User model should look like this:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
....
Make sure you didn't forget to implement the Authenticatable in the model, like:
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
and Model implements it
class User extends Model implements AuthenticatableContract {
use Authenticatable;
...
}

Categories