Laravel 5.5 Spatie Permission doesn't create role - laravel

I'm using laravel 5.5 and spatie / laravel-permission ":" ^ 2.38 ". I created a simple controller for creating a role, but laravel returns the error:
Illuminate \ Database \ Eloquent \ MassAssignmentException
"name"
My simple controller is:
<?php
namespace App;
namespace App\Http\Controllers\UserRole;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use App\Role;
use App\Permission;
use DB;
class RolePermission extends Controller
{
public function create_role(){
Role::create(['name' => 'noc']);
}
}

The problem is not related to Spatie. You must allow the Role object to be mass-assignable. In your case, you must put:
protected $fillable = ['name'];
to your Role model, or:
protected $guarded = [];
See more here

Related

Call to undefined method Auth::user()->can() in laravel 8 error

I have a problem with determining the permission with Auth::user()->can().
For example Auth::user()->can('trunk.index) returns always error;
But I have a problem.
If I dump $user->getPermissionsViaRoles(); ,I will get a large result.
I'm using different table user_view table.
And according to it I have changed in Auth.php file. and login is working fine.
'providers' => [
'users' => [
'driver' => 'self-eloquent',
'model' => App\Models\UserView::class,
]
],
But when I try to check permission the via Auth::user()->can('trunk.index) then it gives error below error.
Call to undefined method App\\Models\\UserView::can()
Below is my UserView model code.
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Traits\HasRoles;
use Laravel\Lumen\Auth\Authorizable;
use Laravel\Sanctum\HasApiTokens;
class UserView extends Model implements AuthenticatableContract
{
use Authenticatable;
use HasFactory;
use HasRoles;
use HasApiTokens;
protected $table = 'user_view';
protected $primaryKey = "user_id";
protected $fillable = [
'username', 'password',
];
protected $guarded = [];
public function getAuthPassword()
{
return ['password' => $this->attributes['user_password']];
}
// public function can($abilities, $arguments = []) {
// }
}
help me if I'm missing something. Thank you.
You are using a custom User model which does not implement the \Illuminate\Contracts\Auth\Access\Authorizable interface and \Illuminate\Contracts\Auth\Access\Gate\Authorizable trait. You do actually have an import for this interface, but it is not used anywhere.
Also be careful about the existing import Laravel\Lumen\Auth\Authorizable. I'm not familiar with Lumen, but this might be a different implementation / wrong import.
You essentially need to update your model with the following two lines:
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Foundation\Auth\Access\Authorizable; // <-- add import
use Spatie\Permission\Traits\HasRoles;
use Laravel\Sanctum\HasApiTokens;
class UserView extends Model implements AuthenticatableContract,
AuthorizableContract // <-- add interface
{
use Authenticatable;
use Authorizable; // <-- add trait
use HasFactory;
use HasRoles;
use HasApiTokens;
// ... other code ...
}

laravel voyager soft delete issue

I'm trying to get soft deleting work on laravel voyager but everytime I delete a post it's also deleted in my database.
here is my post model :
<?php
namespace App;
use TCG\Voyager\Traits\Resizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Category;
class Post extends Model
{
use Resizable;
use SoftDeletes;
protected $dates = ['deleted_at'];
public function category()
{
return $this->belongsTo(Category::class);
}
public function scopeIspublished($query)
{
return $query->where('status','published');
}
}
I added a column deleted_at to my posts table as well.
Thanks for helping

ORM not working in Default UserController & User Model in laravel

I want to get User wise Role. here is I'm facing error ....
UserController.php ( user controller file )
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\Employee;
use App\Role;
use App\User;
use App\Site;
use App\Client;
use App\ProjectType;
use App\UserPermission;
use Auth;
use DB;
use App\Project;
class UsersController extends BaseController {
public function __construct() {
$this->isSetClientAndProjectType();
$data = User::with('Role')->first();
echo "<pre>";print_r(json_decode($data)); die;
}
}
User.php ( user model file )
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes;
use Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get users roles
public function Role() {
return $this->hasMany('App\Role', 'role_id', 'id');
}
}
Error is
How can i solve this error?
Help me guys.
Thank You.
If a user has many "roles" it should be public function roles().
You have defined:
A single user has a role_id
Therefore you need:
If a user has a single role it would be:
public function role() {
return $this->belongsTo('App\Role');
}
The reverse on the Role model would be:
public function users() {
return $this->belongsToMany('App\User');
}
Since many users can have the same role.
Hope this helps.
You need to add belongsTo relationship
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes, Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get user's role
public function role() {
return $this->belongsTo('App\Role');
}
}
Now fetch data
$user = User::with('role')->find(1);
$role = $user->role;
You need to make sure your table structure and foreign key references are compatible with the model relationship methods used.
For example, you have used "HasMany" relationship on User model. For that, you will have to make sure that each record/row in users table(User model) "has many" associated records/rows in roles table(Role model).
Here HasMany method assumes a foreign key "role_id" on roles table(Role Model). On not finding of which, it throws error.
You first need to take in consideration the table structure of roles and users table(User and Role model) as per your requirement, and than add model relationship methods accordingly.
It can be a bit tricky if you are using the methods for the first time, you can refer the laravel documentation for the same:
eloquent-relationships

Class ' not found in laravel 5.0.16

I am beginner in laravel. And I am using Laravel 5.0.16 in my wamp server. I have been learning laravel by free video tutorial available in laracasts.com. I have been trying to fetch data from database. I have checked that my app is already connected to database.
I do have below structure in app folder:
-app
-Http(folder)
-Other folders (folder)
-Article.php (file)
-User.php (file)
In side Http folder:
-Controllers (folder)
-Middleware (folder)
-Requests (folder)
-Kernel.php (file)
-routes.php (file)
In Controllers folder:
-ArticleController.php (file)
Below is code in side routes, controllers and model file:
/*routes*/
Route::get('articles','ArticleController#index');
/*Controller file*/
use App\models\Article;
namespace App\Http\Controllers;
use App\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller {
public function index()
{
$users = Article::all();
return $users;
}
}
/*Model file - Article.php*/
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = 'users';
protected $fillable = ['id','firstname', 'lastname', 'email','reg_date'];
}
Where users is DB table with fields.
I am getting below arror:
FatalErrorException in ArticleController.php line:
Class 'App\Http\Controllers\Article' not found
I have check other SO forums but they didn't help me, can anyone suggest me what am I missing?
There are two issues here. One the namespace declaration should happen before any use statements.
Second your models uses the model namespace but your models aren't in a model directory. The namespace should match the directory structure. So you either need to change the namespace to use App\Article (also change the namespace in the model file) or move the model files into a models directory.
So to fix this without moving files update the code to look like this
namespace App\Http\Controllers;
use App\Article;
use App\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller {
public function index()
{
$users = Article::all();
return $users;
}
}
/*Model file - Article.php*/
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = 'users';
protected $fillable = ['id','firstname', 'lastname', 'email','reg_date'];
}
Add this line to the top of a controller:
use App\models\Article;
Or use the full namespace when working with this model:
\App\models\Article::all();

Class 'App\' not found on laravel 5.2

I want to add data to the database after a successful validation,but i get this error.'
FatalThrowableError in AboutController.php line 51:
Class 'App\About' not found.
My Controller
<?php
namespace App\Http\Controllers;
use App\About;
use Illuminate\Http\Request;
use App\Http\Requests;
class AboutController extends Controller
{
public function store(Request $request)
{
//
$about = $request->about;
$validation = \Validator::make($about, About::$rules);
if($validation->passes())
{
About::create($about);
return route('about/admin')->compact(about);
}
}
my Model
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
//
protected $guarded = array('id');
protected $fillable = array('about');
public static $rules = array('about' => 'required|5');
}
location of controllers and Model:
App\Http\Controllers\AboutController
App\About
I have tried to run
php artisan cache:clear
php artisan clear-compiled
composer dump-autoload
I'm stuck can anyone tell me what is causing this?
As #webNeat said you should change the namespace that you are using in your Model.
Your Model About
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
Controller
<?php
namespace App\Http\Controllers;
use App\About; // You have declared App\Http\Controllers in your Model
Model About Fixed
<?php
namespace App; // change to this namespace
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
If you're a bit lost with Laravel or namespaces I strongly recommend you to use php artisan with each of its commands, and see and study what they do by reading all the code generated. For this case with:
php artisan make:model About
You will get a fresh new About model prepared for receive all your code with the correct namespace.
changing the namespace of your model to App should fix the issue.
<?php
namespace App; // <- here
use Illuminate\Database\Eloquent\Model;
class About extends Model
{

Resources