RareAdmin

RareAdmin documentation version 1.0


Introduction

  • Item Name : RareAdmin Panel | Laravel Admin Panel / CMS / CRUD
  • Item Version : v 1.0
  • Author : techweb3
  • License : License

First of all, Thank you so much for purchasing this script. You are awesome!

This documentation is to help you regarding each step of set-up and customization. Please go through the documentation carefully to understand how this script is made and how to edit this properly. Basic Laravel knowledge is required to use this script. You may learn basics from Laravel DOCS.

Requirements

You will need the following sofwares to customize this template.

  1. Code Editing Software (eg: PHPStorm, Sublime Text or Notepad)
  2. Web Browser for testing (eg: Google Chrome or Mozilla Firefox)
  3. Command line
  4. Meet the Laravel requirements

Installation

This script is based on the Laravel framework and therefore its installation is identical to the installation of Laravel itself.

  1. Copy all files from rareadmin directory to your webserver root (for example C:/wamp64/www/*RAREADMINDIR* or /Applications/MAMP/htdocs/*RAREADMINDIR*)
  2. Open command line, navigate to rareadmin directory and run the following command to install all necessary dependencies:
    composer install
  3. Now open in your favourite text editor .env file and set your database credentials:
                                                DB_CONNECTION = mysql
                                                DB_HOST = 127.0.0.1
                                                DB_PORT = 3306
                                                DB_DATABASE = DATABASE_NAME
                                                DB_USERNAME = DATABASE_USERNAME
                                                DB_PASSWORD = DATABASE_PASSWORD
                                            
  4. In command line, run the following command to migrate the database:
    php artisan migrate
  5. Then seed the necessary data into the database:
    php artisan db:seed
  6. Run the the following command to generate the key:
    php artisan key:generate
  7. ALL DONE, now you can start the web server and show the admin panel:
    php artisan serve

How to Use RareAdmin

With the db:seed command, we also created the first superadmin user, who has permissions to set up new sections in the admin panel and also manage all columns and permissions. So let's log in under his account:

                            E-mail: superadmin@demo.com
                            Password: password
                        

After logging in, you will see the first main section - Dashboard. Here you will see the first two main Entities (Roles and Users).

With the db:seed command, we also created three Roles (user, client and superadmin). Each role has its own permissions. For example, a user role can only View Entity Users, but clients can not only view Users, but also manage (Edit and Create) users, but a client role cannot Delete users. All these rules can be managed with superadmin account in the Permissions section of the Superadmin menu.

If you have prepared the models and run the migrations, we can continue to Automatically Discover Models or Manually bind Entities to RareAdmin panel.

Basic usage

Basic usage

RareAdmin is primarily designed to be able to create an administrative interface from your existing Database and Laravel Models without having to write or set a single line of HTML or CSS or JS code.

Therefore, the only thing you need to have done is the correct backend logic of the models with all the relations defined according to Laravel standards and conventions.

Full Documentation of Laravel Naming Convections

Examples of correct naming of tables and columns:

  • - Model "City" has table "cities"
  • - If the users table has One-To-Many relation to the cities table, the correct column name in the table is "user_id"
  • - The correct name for the Many-To-Many relationship between "users" and the "clients" table is "client_user"
  • - etc...

Creating first Model

Let's say we want to manage cities through our admin panel. The city has a name, short information, a longer description, GPS coordinates, a thumbnail and an activated/deactivated status. All we need to do is create a model and migration:

  1. Open command line and run the following command:
    php artisan make:model City -m
    Laravel will create two new files in our project: A city model and a new migration file.
  2. Edit the City model in *RAREADMINDIR*/app/Models/City.php to the following:
    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class City extends Model
    {
        protected $guarded = ['id'];
    }
  3. Edit the last created migration in *RAREADMINDIR*/database/migrations/0000_00_00_000000_create_cities_table.php to the following:
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::create('cities', function (Blueprint $table) {
                $table->id();
                $table->text('name')->nullable();
                $table->text('short_desc')->nullable();
                $table->text('long_desc')->nullable();
                $table->decimal('lat', 11, 8)->nullable();
                $table->decimal('lng', 11, 8)->nullable();
                $table->text('thumbnail')->nullable();
                $table->boolean('active')->default(false);
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            Schema::dropIfExists('cities');
        }
    };
    
    and then run:
    php artisan migrate
  4. Now we can go to the "Entities" section of the Superadmin menu and click the green +Add button and fill in the following details:
  5. And we are done! Now we can try to add one record to the "City" Entity:

Note: If you can't see the images after creating a new record, make sure you don't forget to run the laravel artisan command to publish the storage directory: php artisan storage:link


Creating One-To-Many Relation

  1. In the previous part, we created a city model with migration. If you started reading this documentation here, create the necessary model according to the previous step: Creating first Model.
  2. To define a One-To-Many Relationship, we just need to define that relationship according to Laravel standards. Let's say we want to add a mayor to each City. To do this, just add a few lines of code to the City Model and also to the User Model:
    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class City extends Model
    {
        protected $guarded = ['id'];
    
        //we will add this method
        public function user() {
            return $this->belongsTo(User::class);
        }
    }
    <?php
    
    ...
    
    class User extends Model
    {
        ...
    
        //we will add this method
        public function cities() {
            return $this->hasMany(City::class);
        }
    
        ...
    }
  3. Then add one column to the cities table. To do this, just create a new migration file using the following command:
    php artisan make:migration add_user_id_to_cities_table --table=cities
  4. Edit the last created migration in *RAREADMINDIR*/database/migrations/0000_00_00_000000_add_user_id_to_cities_table.php to the following:
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::table('cities', function (Blueprint $table) {
                $table->unsignedBigInteger('user_id')->nullable();
                $table->foreign('user_id')->references('id')->on('users');
            });
        }
    
        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            Schema::table('cities', function (Blueprint $table) {
                $table->dropForeign('cities_user_id_foreign');
                $table->dropColumn('user_id');
            });
        }
    };
    
    and then run:
    php artisan migrate
  5. Now we can go to the "Entities" section of the Superadmin menu and click the blue Edit button in "Cities" row and insert one new column (Press green Add button behind the "Columns" title): This step tells RareAdmin that we have added a new user_id column (the first input) that represents the User model (the second input), we want to name it as Mayor (third input), the type is a One-to-Many Relationship (fourth input), and in the cities entity table we want to see it as the email column (fifth input) from the users table (we can also use, for example, the column "name").

  6. Now we can try to assign some demo user as the mayor of our demo entry: London. We need to go to the "cities" section, click the Edit button in the first line of our demo record, and at the bottom of the form, assign some user to this record: After saving that form, we will see successfully assigned mayor to our city. We see the email written there, but if we set "name" in the last field in step 5, the mayor's name will be written there.

Creating Many-To-Many Relation

  1. Similar to the previous steps, we need to define relation according to Laravel standards. Let's say we want to assign categories to cities. So first we need to create a new Category model with migration:
    php artisan make:model Category -m
  2. Edit the created files as follows:
    *RAREADMINDIR*/app/Models/Category.php:
    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Category extends Model
    {
        protected $guarded = ['id'];
    
        public function cities() {
            return $this->belongsToMany(City::class);
        }
    }
    Add these lines of code to *RAREADMINDIR*/app/Models/City.php:
    <?php
    
    ...
    
    class City extends Model
    {
        ...
    
        //we will add this method
        public function categories() {
            return $this->belongsToMany(Category::class);
        }
    
        ...
    }
    and *RAREADMINDIR*/database/migrations/0000_00_00_000000_create_categories_table.php:
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::create('categories', function (Blueprint $table) {
                $table->id();
                $table->text('name')->nullable();
                $table->timestamps();
            });
    
            Schema::create('category_city', function (Blueprint $table) {
                $table->unsignedBigInteger('category_id')->nullable();
                $table->foreign('category_id')->references('id')->on('categories');
                $table->unsignedBigInteger('city_id')->nullable();
                $table->foreign('city_id')->references('id')->on('cities');
            });
        }
    
        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            Schema::dropIfExists('category_city');
            Schema::dropIfExists('categories');
        }
    };
    
  3. Now we can again go to the "Entities" section of the Superadmin menu and click the green +Add button and fill in the following details:
  4. After this step, we created 3 more cities: Paris, Berlin and Dedham.
  5. So now we can create a new category, for example: Megalopolis and assign some cities to it: And we are done!

Other Relation types

As mentioned above, using RareAdmin requires minimal knowledge of how Laravel Relations works. With RareAdmin you can define and manage any type of relationship in any way. RareAdmin currently supports:

  • - One to One
  • - One to Many
  • - One to Many (Inverse) / Belongs To
  • - Has Many Through
  • - Many to Many
  • - Parent
  • - Polymorphic Relationships
  • - Pivot with attributes (must have ID)

Custom Back-end logic

WAIT! And how can I implement my own project logic or backend processes if I can't touch the handling methods in the Controller?

Great question and the Answer is: Laravel Observers

With Laravel Observer, you can handle model events such as save or update.

Quick Example. Let's say you want to set the user's name to uppercase and send an email every time a user's role changes:

<?php

namespace App\Observers;

use App\Models\User;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Mail;

class UserObserver
{
    /**
     * Handle the User "created" event.
     */
    public function created(User $user): void
    {
        if (strlen($user->name) > 1) {
            $user->name = ucfirst($user->name);
            $user->saveQuietly();
        }
    }

    /**
     * Handle the User "updated" event.
     */
    public function updated(User $user): void
    {
        if ($user->isDirty('role_id')) {
            Mail::raw('Role changed!', function (Message $message) use ($user) {
                $message->to($user->email);
            });
        }
    }

    /**
     * Handle the User "deleted" event.
     */
    public function deleted(User $user): void

    ...

}

Copyright and license

Code released under the MIT License.

For more information about copyright and license check MIT License.