CCS3310 Software Engineering Methods – Lesson 5

What we did today (summary)


Terminal command to be typed in: c:/laragon/www
1. Install Laravel (project name is ccs3310):

composer create-project laravel/laravel ccs3310

2. Now open the project folder with VS-Code (or your preferred IDE). Open the .env file and update the database setting as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ccs3310
DB_USERNAME=root
DB_PASSWORD=

 3. From the terminal type the following to goto the project folder:

cd ccs3310

 4. Then type the following to migrate the db tables and start the Laravel dev server:

php artisan migrate
php artisan serve

 Open a browser and goto localhost:8000, here you should see Laravel running successfully

Read the next steps here to understand how the MVC architecture works: https://laravel.com/docs/11.x#next-steps

Excercise

You need to create the following:

1. Artisan command to create model, migration & controller together

php artisan make:model -mc Book –resource

this will create:

  • Models in → \app\Models\
  • Database migrations in → \database\migrations\
  • Controller in → \app\Http\Controllers\

2. Add resource routes

Open → \routes\web.php

add the following:

use App\Http\Controllers\BookController;
...
Route::resource('books', BookController::class);

3. Open the controller at → App\Http\Controllers\BookController.php

add the following line to the index method:

public function index()
    {
        echo "Hello From the Book Controller -> index method";
    }

Naming convetions

  • Controllers → ProductController

  • Models (singular) → Product or BookReview

  • Controller/Model property → product_id

  • Controller/Model method → getAll()

  • Variables → productTypeflat

  • Blade files (snake_case)→ index.blade.php (activity_log, show, create, import, export etc…)

  • Modules (plural) → Dealers

  • Database fields → snake_case

    • Primary keys are always named id whatever the data type is.

    • Timestamps are past tense, suffixed …at (eg: created_at, updated_at)

    • Date, Time, and Datetime are not timestamps, name them freely.

    • Booleans are always positive and start with is_… (eg: is_admin)

    • Numeric columns should be plural, like guesses or failures

    • Lists as JSON columns are plural, and may end with …_list

    • Complex JSON trees can be named freely, but may end with …_tree

    • Foreign columns always end with …_id, like author_id

    • Foreign columns using non-id foreign columns can be suffixed as …_as_id

    • Multiple text columns may end with …_body

    • Multiple text columns for binary-encoded data may end with …_data

    • Binary columns always end with …_blob or …_binary

    • Columns for encoded data always end with …_encoded

    • Columns for encrypted data always end with …_encrypted

  • Database table (last word will be plural) → products or book_reviews

  • Routes: Use dashes in the URI pattern but camelCase for the route parameter, as that is what Laravel will look for if trying to do route–model binding. So:

Route::get('ip-addresses/{ipAddress}', 'IpAddressController@index');

Recorded lesson:


References:

Laravel is a powerful framework, and the Laravel request lifecycle is the best place to start when you want to learn more about the framework itself.


All lessons > 

Share