auth Flashcards
1
Q
setup in terminal
A
composer require laravel/ui php artisan ui bootstrap --auth npm install npm run dev npm run watch
2
Q
Gate Method
A
Go to Provides/AuthServiceProvider
public function boot()
{
$this->registerPolicies();
Gate::define(‘keyname’,function (User $user,Post $post){
return $user->id == $post->user_id;
});
}
In Controller file
Input Use use Illuminate\Support\Facades\Gate;
public function edit($id){ $post=Post::find($id); if(Gate::allows('keyname',$post)){ Post::find($id)->update([ "title"=>"New Title" ]); } else{ abort(403); }
3
Q
Policy Method
A
First -php artisan make:policy Postpolicy
Register in AuthServiceProvider File
‘App\Models\Post’ => ‘App\Policies\Postpolicy’
IN Postpolicy file
public function update(User $user,Post $post){
return $user->id == $post->user_id;
In PostController Method public function edit($id){ $post=Post::find($id); if(Gate::allows('update',$post)){ Post::find($id)->update([ "title"=>"New Title" ]); } else{ abort(403); } }
4
Q
jetstream auth
A
composer require laravel/jetstream
php artisan jetstream:install livewire