Code Monkey home page Code Monkey logo

seamless-admin's Introduction

Seamless Admin Panel

A seamless Django-like admin panel setup for Laravel. Simple, non-cms table manager for admins.

Latest Stable Version License Tests

Installation steps

  1. Require the Package

Run the following command to install the package into your Laravel application:

composer require advaith/seamless-admin
  1. Publish assets and config files with:
php artisan vendor:publish --provider="Advaith\SeamlessAdmin\SeamlessAdminServiceProvider"

This will create a seamless.php configuration file within your config folder. This will also publish all the assets related to this package.

Note: If you find the UI or the admin functionalities are not working properly after a composer update, run this command again with the flag --tag="assets" to republish the assets. This will override the existing assets with the new ones.

Usage

Use the provided trait, SeamlessAdmin, in any of your models to get started with the package. Example:

<?php

namespace App\Models;

use Advaith\SeamlessAdmin\Traits\SeamlessAdmin;
...

class Post extends Model
{
    use SeamlessAdmin;
    ...
}

Et Voila! That's all you have to do to get started. Visit /admin to access the admin dashboard after logging in.

Screenshot

Configuration

seamless-admin.php config file

  • prefix: Change the prefix of the admin routes. However, this will not change the name of the route. By default, /admin will be used as the prefix
  • api_prefix: Change the prefix for api routes.
  • middleware: Global middleware for the admin routes. By default, the auth middleware is set.
  • api_middleware: Middleware group for api routes.
  • layout: You can customize the layout that the admin pages use by changing this option. Make sure that you yield appropriately to include dynamic contents.

Model specific configuration

All the model specific configuration should go inside the respective model file.

Properties

  • public bool $hasAdminPage: Set this to false to prevent the model from showing up on the admin page. Advantage of using this instead of removing the trait from the model is when other models try to check for foreign keys this will be used as a hint to fetch other columns.

  • public string $adminIcon: We use feathericons as the icon provider. Type in the name of the icon you want to use.

  • public string $adminGroup: Add a value to change the group in which the model should be displayed in the sidebar.

  • protected $primaryKey: Primary key of the model will be used even if it is not id wherever it is needed.

  • protected $fillable: adminIndexFields uses data from the $fillable and $hidden variable to show default values

  • protected $hidden: adminIndexFields uses data from the $hidden and $hidden variable to show default values

Methods

public function adminIndexFields(): array

Admin index page will display all the contents of protected $fillable excluding protected $hidden by default. To override this, use the method adminIndexFields and return an array of fields.

public function adminIndexFields(): array
{
    return [
        'title',
        'content'
    ];
}

public function adminOnCreate(array $fields): array

Use the method to change the data saved on create. An example is for Users model:

public function adminOnCreate(array $fields): array
{
    return [
        ...$fields,
        'password' => bcrypt($fields['password'])
    ];
}

This method can also be used as a hook as it fires just before the entry is created.


public function adminOnEdit(array $fields): array

Use the method to change the data saved on edit. It functions similar to the adminOnCreate method.


public function adminCanAccessIndex(): bool

Determine whether the logged-in user has the privilege to access the model data. Disabling access to index page will remove access to all the functionalities (view, create, edit, delete) for the current user.


public function adminCanAccessCreate(): bool

Determine whether the logged-in user has the privilege to create a new entry.


public function adminCanAccessEdit(): bool

Determine whether the logged-in user has the privilege to edit an entry from the database.


public function adminCanAccessDelete(): bool

Determine whether the logged-in user has the privilege to delete data from the database.


public function __toString(): string

A magic method to convert the model instance into a string. By default, the field from $fillable is used. Usage example:

public function __toString(): string
{
    return "{$this->firstName} {$this->lastName}";
}

Hooks

  • public function adminEdited(): void: This hook is fired when a model is edited
  • public function adminCreated(): void: This hook is fired when a model is created

Adding a custom page

To add a custom page to the sidebar use the SeamlessAdmin Facade in your application's AppServiceProvider. Example:

<?php

namespace App\Providers;

use Advaith\SeamlessAdmin\Facades\SeamlessAdmin;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    ...
    public function boot()
    {
        SeamlessAdmin::add('files.index', 'File', [
            'isAllowed' => fn() => auth()->user()->can('manage_files'),
            'icon' => 'layout',
            'group' => 'Blog'
        ]);
    }
}

The add method takes 3 arguments:

  1. The route name. This is the route name that you have defined in your routes file. Note: This is not url itself but instead the name of the route.
  2. Alias for the route. This is the name that will be displayed in the sidebar of the admin page.
  3. An optional array which consists of
    • isAllowed: Whether the route is visible to the current user
    • icon: Icon to be displayed in the sidebar. We use feathericons
    • group: Group in sidebar to which the custom route should be added.

To fully utilize the custom page, extend the layout seamless::layout in your blade file. Exmaple:

@extends('seamless::layout')

@section('title', 'Title')

@section('header')
    <!-- This section will be appened to the header -->
@endsection

@section('content')
    <div class="container px-4 py-2">
        The actual content here
    </div>
@endsection

@section('footer')
    <!-- This section will be added to the bottom of the page -->
@endsection

More configuration options will be added soon. For requesting a new feature, create a new issue with the label feature-request.

seamless-admin's People

Contributors

advaith3600 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.