Laravel CMS | N-04 | Migration, Models and Adding Categories

Subscribe to my newsletter and never miss my upcoming articles

Video Tutorial


This is the Fourth Tutorial in the Series of making a Control Management System on top of laravel, so if you are new make sure you watch the last tutorials before continuing on the series, We have gone through the basic things in laravel controllers, assets and views/routing but there still some topics need to be covered before you can build a valuable application, So in this tutorial we are going to talk about Migrations and Why they are very helpful for us also dealing with Models and the importance of using routes alongside controllers.


Almost Any WebApp/WebSite uses a DataBase for dealing with data, for that there is what called migrations that make your life easier especially when deploying your application to the production server, So Migrations used to structure your database by creating tables/columns automatically for you so you don’t need to remember you chunk of tables structure every single time, implement it once and you are good to go.

First, you need to create a database (MySQL) in our case, after that open .env file on your app folder and edit Database Configuration to your database Name, Username, and Password

# You Change database connection to your favorite, see laravel DOCS for more info on the available drivers


Check here for the available Laravel Database Drivers.

now after configuring your database, create a migration file and that using artisan and the command line

php artisan make:migration create_category_table

we are creating the category table here, for our CMS so naming it the same as it purpose makes a huge difference.

go into database/migrations and open up your migration file, there will be an up() and Down() function one responsible for creating table and columns and the other for removing it, you get the point, you should edit it as follows:

public function up()
     Schema::create('category', function (Blueprint $table) {
        $table->string('name', 200);
        $table->string('author', 100);

Check out laravel DOCS about the blueprint available methods for the different kind of variables

now to migrate your table into the database run

php artisan migrate

and your table should be in the database


Now you may be wondering, how can I reach the data in the database? and that why Models exist, So Models are an instance of a database table of your choose (By Default the table going to be the same as your model name), Create a Model By typing

php artisan make:model Category 
# For a naming Convention we use a Singular Version and a Capitale Letter!

Check out your app folder, your model should be there. Now to specify the table it should reference add

//Make Sure your are inside your Model Class Scope 
protected $table = "category";


For Adding new Categories we need Forms and instead of using regular HTML Forms we use modern laravel forms, and there is a library called Laravel Collective will help you achieving that, so add it with composer

composer require "laravelcollective/html":"^5.4.0"

replace the version with the latest one, that will install the required files, but still, one thing needs to be done.

go to config and open up app.php, scroll all the way down to providers and add

'providers' => [
  // ...
  // ...

and on Aliases, add

'aliases' => [
  // ...
    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,
  // ...

you check out the OFFICIAL Installation Page for more Information on the Library.

Now What left for us is just creating the form and Handle adding categories.

Adding Categories

go into the category.blade.php (make sure to create it if you haven’t already), and add a form using Collective

{!! Form::open(['route' => "category.add", 'method' => 'POST']) !!}

    {!! Form::label('categoryName', 'Name') !!}

    {!! Form::text('categoryName', '', ['class' => 'form-control', 'placeholder' => 'Category 

Name']) !!}

    {!! Form::submit('addCategory', ['class' => 'btn btn-success btn-block']) !!}

{!! Form::close() !!}

you can notice this is pretty much the same as using the regular forms syntax, So you should understand it without issues.

now since we have specified the route (category.add) we need to register it within our engine, so in the web.php add the post method

Route::post('/addCategory', 'CategoryController@store')->name('category.add');

and that suppose that you have already created the CategoryController with — resource option for quick main functions setup.

go to the Category controller and in the store(Request $request) function we gonna put our category adding logic, make sure the function takes a Request Parameter, so it can receive Form Data.

Here is the full snippet of the controller class

* Store a newly created resource in storage.
* @param  \Illuminate\Http\Request  $request
* @return \Illuminate\Http\Response
public function store(Request $request)
  //Initialize The Category 
  $category = new Category();
  $category->name = $request->categoryName;
  $category->author = "Islem Penywis";


  return redirect()->back();

Notice: Make Sure to include the Model, by adding it at the top of class (Before the Class’s Scop)

//Include Your Model 
use App\Category

No Going to The Category Page and Trying to add a Category named for e.x testCategory will add it to the database, but we won’t notice that since there are no success/error messages or table of Categories that we can view on it the currently available Categories.

We Will implement that in the next Tutorial/Video, So Stay Tuned.

If you are feeling a bit overwhelmed by this tutorial, please make sure to watch the tutorial above for more clarifications.

No Comments Yet