Laravel Tutorial #4: Admin Panel

Please note that this post may contain affiliate links, and for every purchase you make, at no extra cost to you, a commission fee will be rewarded to me.

Every website requires an admin panel. Unfortunately, unlike Django, Laravel does not include a built-in admin panel, but there are still lots of other choices.

In this tutorial, we’ll use Laravel Nova and Voyager as examples. Nova is the official admin panel for Laravel apps, created by the same team that developed the Laravel framework. While if you prefer a free admin panel, Voyager is a good choice.

Laravel Nova

Download Laravel Nova

First, you need to register a new account and purchase a license. The solo license is $99 per project, and it includes all features of Laravel Nova. This part should be pretty straightforward.

After that, go to the “Releases” page, and download the latest version.

Install Laravel Nova

Uncompress the file you just downloaded, you should get a folder named something like laravel-nova-fd55xxxxxxxxxxxxxxxxxxc5b5. Rename it into nova, and put it in the root directory of our project like this:

Once you are done, we need to update the composer.json file. Add the following configuration:

"repositories": [
    {
        "type": "path",
        "url": "./nova"
    }
],

Now, in the require section, add laravel/nova like this:

"require": {
    "php": "^7.2.5",
    "fideloper/proxy": "^4.2",
    "laravel/framework": "^7.0",
    "laravel/nova": "*"
},

After the composer.json file has been updated, run the update command in the terminal.

composer update

Finally, run the nova:install and migrate Artisan commands. The nova:install command will install Nova’s service provider and public assets within your application:

php artisan nova:install

php artisan migrate

After that, we need to check if the Nova service provider has been registered. Open config/app.php and scroll down to the App Service Provider section. If you don’t see NovaServiceProvider, add the following code:

App\Providers\NovaServiceProvider::class,

Open the browser and go to http://localhost/nova. You should see the login page.

Create A New User

However, we still need a new user account to log in. We can add one using the nova:user artisan command:

php artisan nova:user

You will be prompt to input you name, email, and password. After that, you should be able to log in with the account.

Define Resources

Now, we need to integrate Nova with our Laravel application by defining “Resources”.

In Laravel Nova, each “resource” corresponds to a Eloquent model in our blog app. By default, Nova resources are stored in the app/Nova directory of our application. You may generate a new resource using the nova:resource Artisan command:

php artisan nova:resource Post

The resource this command generated corresponds to the Post model of our blog application. For now, we only care about $title, $search, and fields().

$title defines the value that should be used when the resource is being displayed. For example, when you try to search for a post in Nova, you would want the “title” of the post to be displayed, instead of just an id.

As for $search, we would want to search by title, meta title, or content. Add the following code in the resource:

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'title';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'title', 'content',
    ];

And for the last part, fields(), we need to talk about it in detail later.

Voyager

Install Voyager

First, include the Voyager package using composer:

composer require tcg/voyager
img

The following command will install Voyager:

php artisan voyager:install
img

If you want some dummy data for our project, use this command instead:

php artisan voyager:install --with-dummy

Installation complete.

img

Next, we need to create a new admin user:

php artisan voyager:admin your@email.com --create

Follow the instructions and type in your user name and password.

img

Start the server and make sure it is running on port 8000. Run the following command to start the Laravel development server:

php artisan serve

Or if you prefer using XAMPP, open the apache configuration file and make these changes:

img
img

This will make Apache serve on port 8000. Finally, visit the URL http://localhost:8000/admin in your browser, and log in with your credentials.

Although Voyager is not as powerful as Nova, it is much easier to setup and use. Once you installed it, you don’t need to write a single line of code.

Image Not Loading

This is a common problem with Voyager if you are using XAMPP, and it is really easy to solve. Edit the .env file. Change APP_URL to http://localhost:8000.

img

3 thoughts on “Laravel Tutorial #4: Admin Panel”

  1. My Laravel version:
    “require”: {
    “php”: “^7.3”,
    “fideloper/proxy”: “^4.2”,
    “fruitcake/laravel-cors”: “^2.0”,
    “guzzlehttp/guzzle”: “^7.0.1”,
    “laravel/framework”: “^8.0”,
    “laravel/tinker”: “^2.0”,
    “tcg/voyager”: “^1.4”
    },

    Error installing Voyager:
    Downloading (100%)
    doctrine/cache suggests installing alcaeus/mongo-php-adapter (Required to use le
    gacy MongoDB driver)
    intervention/image suggests installing ext-imagick (to use Imagick based image p
    rocessing.)
    intervention/image suggests installing intervention/imagecache (Caching extensio
    n for the Intervention Image library)
    tcg/voyager suggests installing laravel/legacy-factories (Required to run Voyage
    r tests with Laravel 8.x)
    Writing lock file
    Generating optimized autoload files
    > Illuminate\Foundation\ComposerScripts::postAutoloadDump
    > @php artisan package:discover –ansi

    Doctrine\DBAL\Driver\PDO\Exception

    SQLSTATE[HY000] [2002] Iiaee??aiea ia onoaiiaeaii, o.e. eiia?iue eiiiu?oa? ioa
    a?a cai?in ia iiaee??aiea.

    at C:\wamp64\www\chords\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDO\Exce
    ption.php:18
    14aЦХ final class Exception extends PDOException
    15aЦХ {
    16aЦХ public static function new(\PDOException $exception): self
    17aЦХ {
    az? 18aЦХ return new self($exception);
    19aЦХ }
    20aЦХ }
    21aЦХ

    1 C:\wamp64\www\chords\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConn
    ection.php:41
    Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))

    2 [internal]:0
    Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(
    App\Providers\AppServiceProvider))
    Script @php artisan package:discover –ansi handling the post-autoload-dump even
    t returned with error code 1

    Installation failed, reverting ./composer.json to its original content.

    1. Please check your App\Providers\AppServiceProvider, make sure there are no mistakes. If that doesn’t work, go to config/app.php, it could be because you added the service provider for the package that is not installed in the system.

Leave a Reply

Your email address will not be published. Required fields are marked *