loading...
Cover image for Laravel 8 Tutorial #4: Admin Panel

Laravel 8 Tutorial #4: Admin Panel

ericnanhu profile image Eric Hu Originally published at techjblog.com Updated on ・6 min read

You can download the source code of this tutorial here: https://www.techjblog.com/index.php/laravel-tutorial-for-beginners/

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.

img

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

img

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:

img

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

"repositories": [
    {
        "type": "path",
        "url": "./nova"
    }
],
Enter fullscreen mode Exit fullscreen mode

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": "*"
},
Enter fullscreen mode Exit fullscreen mode

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

composer update
Enter fullscreen mode Exit fullscreen mode

img

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
Enter fullscreen mode Exit fullscreen mode

img

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,
Enter fullscreen mode Exit fullscreen mode

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

img

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
Enter fullscreen mode Exit fullscreen mode

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

img

img

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
Enter fullscreen mode Exit fullscreen mode

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',
    ];
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

img

The following command will install Voyager:

php artisan voyager:install
Enter fullscreen mode Exit fullscreen mode

img

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

php artisan voyager:install --with-dummy
Enter fullscreen mode Exit fullscreen mode

Installation complete.

img

Next, we need to create a new admin user:

php artisan voyager:admin your@email.com --create
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Related Articles

How to Make Your Server More Secure

Laravel Tutorial For Beginners

Django Tutorial For Beginners

Build A Unit Converter with Vue.js

Discussion

pic
Editor guide