PHP code example of mr-timofey / laravel-admin-api
1. Go to this page and download the library: Download mr-timofey/laravel-admin-api 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/ */
mr-timofey / laravel-admin-api example snippets
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Http\Request;
use MrTimofey\LaravelAdminApi\Contracts\ConfiguresAdminHandler;
use MrTimofey\LaravelAdminApi\ModelHandler;
class Post extends Model implements ConfiguresAdminHandler
{
protected $casts = [
'published' => 'bool'
];
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class);
}
public function scopeOnlyDrafts(Builder $q): Builder
{
return $q->whereRaw('draft IS TRUE');
}
public function configureAdminHandler(ModelHandler $handler): void
{
$handler->setTitle('Posts')
->setCreateTitle('Creating new post')
// placeholders can be used, see more: https://mr-timofey.gitbooks.io/vue-admin/placeholders.html
->setItemTitle('Editing post #{{ id }}')
// allow only these methods (everything is allowed by default)
->allowActions(['index', 'item', 'create', 'update', 'destroy'])
// ...or use policies instead
->usePolicies(true,
// optional policy method prefix (Policy::adminActionIndex, Policy::adminActionCreate, etc.)
'adminAction')
->addPreQueryModifier(function(Builder $q, Request $req): void {
// modify index query just after Model::newQuery() is called
$user = $req->user();
if ($user->role !== 'god') {
$q->where('author_user_id', $user->getKey());
}
})
->addPostQueryModifier(function(Builder $q,Request $req): void {
// modify index query just before execution
// useful if you want to set default sort order
$q->orderByDesc('created_at');
})
// automatically search with LIKE
->setSearchableFields(['title', 'summary'])
// ...or/and set custom search callback
->setSearchCallback(function(
Builder $q,
Request $req,
array $searchableFields): void {
$q->searchLikeAGod($req->get('search'));
})
// index page filters
->setFilterFields([
// auto relation filter
'category',
// see more about available prefix modifiers in ModelHandler::applyFilters phpdoc
'>~created_at' => [
'title' => 'Created after',
'type' => 'datetime'
],
// checkbox, applies scopeOnlyDrafts when checked
'drafts' => [
'scope' => 'onlyDrafts',
'type' => 'switcher'
]
])
// index page table columns
->setIndexFields([
'id',
'title',
// will be automatically formatted as datetime if $this->timestamps === true
// or if $this->dates includes 'created_at' field
'created_at',
// you can provide only title this way
'updated_at' => 'Last update date and time'
])
// item creating/editing form fields
->setItemFields([
'title',
// this just works
'category', // categories should be added to api_admin.models config
// this should just work as well but we want some customizations
'tags' => [
'title' => 'Attach tags',
// 'type' => 'relation', // not necessary if field name is same as a relation method
// 'entity' => 'tags', // tags should be added to api_admin.models config
// placeholders can be used, see more: https://mr-timofey.gitbooks.io/vue-admin/placeholders.html
'display' => '{{ name }}',
// relation widget will allow user to create new tags in-place
'allowCreate' => true,
// this field will be filled with the widget's search box input text
'createField' => 'name',
// fill some other fields with fixed values while creating new tag
'createDefaults' => ['description' => 'New tag'],
// customize suggestions query
'queryParams' => [
'sort' => ['sort' => 'asc']
]
],
'content' => ['type' => 'wysiwyg'],
// $casts => ['published' => 'bool'] will automatically set the right field type for you (checkbox)
'published'
])
// creating/editing validation rules (use $this to refer to the currently editing model instance)
->setValidationRules([
'tags' => ['array', 'between:3,8'],
'category' => ['
namespace MrTimofey\LaravelAdminApi\Events;
// abstract base class for all events (holds user identifier)
ModelEvent::class;
// single model instance action events
SingleModelEvent::class; // abstract base class for single model instance action events (holds item identifier)
ModelCreated::class;
ModelUpdated::class; // holds attributes changes, more info in phpdoc of this class
ModelDestroyed::class;
// bulk destroy (holds destroyed instances' identifiers)
BulkDestroyed::class;