Level 73
In your Livewire component you need to use this trait:
use AuthorizesRequests;
and then you call $this->authorize and not on the user instance.
1 like
I am getting this error
Call to undefined method App\Models\User::authorize()
When submitting the livewire form. What am i missing?
User.php
<?php
namespace App\Models;
use App\Models\Item;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Quotation;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Jetstream\HasProfilePhoto;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use SoftDeletes;
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
protected $guarded = [];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'profile_photo_url',
];
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function hasAnyRole(String $role)
{
return null !== $this->roles()->where('name', $role)->first();
}
public function hasAnyRoles(array $role)
{
return null !== $this->roles()->whereIn('name', $role)->first();
}
}
AuthServiceProvider.php
<?php
namespace App\Providers;
use App\Models\User;
use App\Policies\UserPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
// protected $policies = [
// 'App\Models\User' => 'App\Policies\UserPolicy',
// ];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('user-edit', function($user) {
return $user->hasAnyRoles(['Manager']);
});
}
}
Livewire Class - UserForm.php
<?php
namespace App\Http\Livewire;
use App\Models\User;
use App\Models\Company;
use Livewire\Component;
use Illuminate\Validation\Rule;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class UserForm extends Component
{
public User $user;
public $noCompany;
protected function rules()
{
return [
'user.name' => ['required', Rule::unique('users', 'name')->ignore($this->user->id)],
'user.email' => [Rule::unique('users', 'email')->ignore($this->user->id)],
'user.title' => 'nullable',
'user.first_name' => 'required',
'user.last_name' => 'nullable',
'user.mobile' => 'nullable',
'user.phone' => 'nullable',
'user.is_customer' => 'boolean',
'user.is_employee' => 'boolean',
'user.is_provider' => 'boolean',
];
}
// public function updatedUserEmail()
// {
// $this->validateOnly(['user.email']);
// }
//For real-time validation
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
public function mount(User $user)
{
$this->user = $user ?? new User();
}
public function save()
{
//dd($this);
$this->validate();
//New User - Create User
if($this->user->id == null)
{
User::create([
'name' => $this->user->name,
'email' => $this->user->email,
'password' => bcrypt('password'),
'title' => $this->user->title,
'first_name' => $this->user->first_name,
'last_name' => $this->user->last_name,
'mobile' => $this->user->mobile,
'phone' => $this->user->phone,
'is_customer' => $this->user->is_customer ?? false,
'is_employee' => $this->user->is_employee ?? false,
'is_provider' => $this->user->is_provider ?? false,
]);
if($this->noCompany == 1)
{
Company::create([
'name' => $this->user->name . "(Self)",
'is_self' => true,
]);
// /dd($this->user);
$companyId = Company::latest()->first()->id;
$user = User::latest()->first();
$user->companies()->attach($companyId);
}
}//Existing User - Edit User
else{
$this->user->authorize('user-edit');
$this->user->fill([
'name' => $this->user->name,
'email' => $this->user->email,
'title' => $this->user->title,
'first_name' => $this->user->first_name,
'last_name' => $this->user->last_name,
'mobile' => $this->user->mobile,
'phone' => $this->user->phone,
'is_customer' => $this->user->is_customer ?? false,
'is_employee' => $this->user->is_employee ?? false,
'is_provider' => $this->user->is_provider ?? false,
]);
$this->user->save();
}
return redirect()->route('employees.users.index');
}
public function render()
{
return view('livewire.user-form');
}
}
In your Livewire component you need to use this trait:
use AuthorizesRequests;
and then you call $this->authorize and not on the user instance.
Please or to participate in this conversation.