PHP code example of yaroslawww / laravel-user-adminboard
1. Go to this page and download the library: Download yaroslawww/laravel-user-adminboard 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/ */
yaroslawww / laravel-user-adminboard example snippets
Route::middleware(['auth',])
->prefix('dashboard')
->group(function () {
\UserAdmin\Facades\UserAdmin::routes();
// ....
})
// app/UserAdmin/Index/PostsIndexPage.php
namespace App\UserAdmin\Index;
use App\UserAdmin\Index\Resources\PostResource;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use UserAdmin\IndexPage\Actions\InlineActions\RedirectInlineAction;
use UserAdmin\IndexPage\Columns\TextColumn;
use UserAdmin\IndexPage\IndexPage;
class PostsIndexPage extends IndexPage
{
public static string $name = 'posts';
public ?string $identifier = null;
/**
* @inerhitDoc
*/
public string $responseResource = PostResource::class;
public function columns(): array
{
return [
( new TextColumn('Title', 'title') )
->sortable(),
// ...
];
}
/**
* @inheritDoc
*/
public function responsePerPage(Request $request, $query): ?int
{
return 20;
}
public function query(Request $request)
{
$request->validate([
'search' => ['nullable', 'string', 'max:200'],
]);
$query = Post::query();
if ($request->has('search')) {
$text = $request->input('search');
$query->where(function (Builder $query) use ($text) {
$query->orWhere('title', 'like', "%{$text}%");
});
}
return $query;
}
/**
* @inheritDoc
*/
public function inlineActions(): array
{
return [
( new RedirectInlineAction('edit') )
->setIcon('<icon-edit class="h-4"></icon-edit>')
->setTitle('Edit'),
( new RedirectInlineAction('preview') )
->setIcon('<icon-external-link class="h-4"></icon-external-link>')
->setTitle('Preview'),
];
}
}
// app/UserAdmin/Index/Resources/PostResource.php
namespace App\UserAdmin\Index\Resources;
use App\Models\Post;
use UserAdmin\Http\Resources\ListResource;
class PostResource extends ListResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request)
{
/** @var Post $post */
$post = $this->resource;
return [
'id' => $post->getKey(),
'title' => $post->title,
/* Actions data */
'actions_meta' => [
'edit' => [
'url' => $post->editUrl(),
],
'preview' => [
'url' => $post->frontendUrl(),
],
],
];
}
}
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// ...
UserAdmin::indexPage()->usePages([
PostsIndexPage::class,
]);
}
}
shell
php artisan vendor:publish --provider="UserAdmin\ServiceProvider" --tag="config"