1. Go to this page and download the library: Download marcot89/laravel-bullet library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
marcot89 / laravel-bullet example snippets
namespace App\Http\Controllers;
use MarcoT89\Bullet\Controllers\ResourceController;
class UserController extends ResourceController
{
}
// routes/web.php
Route::middleware('auth', function () {
Bullet::namespace('Resources'); // the default namespace is App\Http\Controllers
});
class UserController extends ResourceController
{
protected $only = ['index', 'show'];
// or
protected $except = ['destroy'];
}
// You can set only one middleware for all actions
protected $middleware = 'auth';
// or many middlewares
protected $middleware = ['auth', 'verified'];
// or customized for different actions
protected $middleware = [
'auth',
'auth:api' => ['except' => 'index'],
'verified' => ['only' => ['store', 'update', 'destroy']]
];
class UserController extends ResourceController
{
protected $policy = \App\Policies\CustomUserPolicy::class;
}
use App\Http\Requests\MyCustomUserRequest;
class UserController extends ResourceController
{
protected $requests = [
'store' => MyCustomUserRequest::class,
];
}
class UserController extends ResourceController
{
protected function beforeStore($request, $attributes)
{
return $this->validate($request, [
'name' => '
class UserController extends ResourceController
{
protected function beforeStore($request, $attributes): array
{
$attributes['password'] = bcrypt($attributes['password']);
return $attributes;
}
}
use App\Events\UserCreated;
class UserController extends ResourceController
{
protected function afterStore($request, $user)
{
event(new UserCreated($user));
}
}
// Hooks for index action
protected function beforeIndex($request);
protected function afterIndex($request, $builder);
// Hooks for store action
protected function beforeStore($request, $attributes): array; // should return the attributes for the model being stored.
protected function afterStore($request, $model);
// Hooks for update action
protected function beforeUpdate($request, $attributes, $model): array;
protected function afterUpdate($request, $model);
// Hooks for destroy action
protected function beforeDestroy($request, $model);
protected function afterDestroy($request, $model);
// Hooks for show action
protected function beforeShow($request, $model);
protected function afterShow($request, $model);
// Hooks for edit action
protected function beforeEdit($request, $model);
protected function afterEdit($request, $model);
// Hooks for forceDelete action
protected function beforeForceDelete($request, $model);
protected function afterForceDelete($request, $model);
// Hooks for restore action
protected function beforeRestore($request, $model);
protected function afterRestore($request, $model);
use App\Http\Resources\UserResource;
class UserControlle extends ResourceController
{
protected $resource = UserResource::class;
}
protected function allowedAppends()
{
return ['is_admin', 'is_published'];
}
protected $defaultPerPage = 15; // defaults to 15
protected $maxPerPage = 500; // defaults to 500
protected $searchable = true; // defaults to true
protected function getQuery()
{
return team()->users();
}
protected function getQuery($request); // for index action
protected function getStoreQuery($request);
protected function getUpdateQuery($request);
protected function getDestroyQuery($request);
protected function getShowQuery($request);
protected function getEditQuery($request);
protected function getForceDeleteQuery($request);
protected function getRestoreQuery($request);
public function update($id)
{
$user = User::findOrFail($id);
...
}
class UserController extends ResourceController
{
public function reports()
{
...
}
}
use App\Models\User;
use App\Http\Requests\MyCustomRequest;
class UserController extends ResourceController
{
public function postReports(MyCustomRequest $request, User $user, $report, $param1, $param2) // as many as you want.
{
...
}
}