Larave PHP/MySQL/PHPMyAdmin JSON API.

This is a an API example using Laravel. On this example, we will be using Laravel 8 (as of January 2021)

Requirements:

  1. Docker
  2. Visual Code
  3. Laravel 8
  4. Linux or MacOS [I am using Ubuntu 20 TLS for this tutorial]

Change Directory to where you want to place this project, for this example, I will creating it in my home directory.

$ cd ~

Create a new Lavarel Project

$ curl -s https://laravel.build/APPNAME | bash

Rename the .env.example file to .env and populate the database connection properties

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=api5
DB_USERNAME=root
DB_PASSWORD=

Edit or Create a file called docker-composer.yml in the project's root directory and copy and paste the following code:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            # - pgsql
            - redis
            # - selenium
    # selenium:
    #     image: 'selenium/standalone-chrome'
    #     volumes:
    #         - '/dev/shm:/dev/shm'
    #     networks:
    #         - sail
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping"]
#    pgsql:
#        image: postgres:13
#        ports:
#            - '${FORWARD_DB_PORT:-5432}:5432'
#        environment:
#            PGPASSWORD: '${DB_PASSWORD:-secret}'
#            POSTGRES_DB: '${DB_DATABASE}'
#            POSTGRES_USER: '${DB_USERNAME}'
#            POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
#        volumes:
#            - 'sailpostgresql:/var/lib/postgresql/data'
#        networks:
#            - sail
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
    # memcached:
    #     image: 'memcached:alpine'
    #     ports:
    #         - '11211:11211'
    #     networks:
    #         - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
        
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        restart: always
        container_name: phpmyadmin
        depends_on: 
            - mysql
        ports: 
            - "8081:80"
        environment: 
            PMA_HOST: mysql
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        networks: 
            - sail



networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
#    sailpostgresql:
#        driver: local
    sailredis:
        driver: local

We are going to be using sail for our docker container. The first thing to do is to create a alias to make our terminal commands easier:

$ alias sail="./vendor/bin/sail"

Start Laravel in a docker container.

$ sail up -d

After the download and the laravel project has started, you can open the project in your broswer to confirm is working:

http://localhost

create: Models>Tareas.php

php artisan make:model Tareas

create: migrations>database>seeders>TareasTableSeeder.php

$ php artisan make:seeder TareasTableSeeder

create: Http>Controllers>TareasController.php

$ php artisan make:controller TareasController


create: migrations>database>migrations>2021_01_27_042427_create_tareas_table.php

$ php artisan make:migration create_tareas_table --table=tareas

Tareas.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Tareas extends Model
{
    use HasFactory;
    public $timestamps = false;
}

xxxx_create_tareas_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTareasTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tareas', function (Blueprint $table) {
            $table->string('title')->nullable();
            $table->string('description')->nullable();
            $table->integer('done')->nullable();
            $table->integer('category_id')->nullable();
            $table->integer('user_id')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('tareas', function (Blueprint $table) {
            //
        });
    }
}

TareasTableSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class TareasTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        for ($i=0; $i < 50; $i++) {
            
            DB::table('tareas')->insert([
                'title' => Str::random(10),
                'description' => 'Exercitation cillum adipisicing qui laborum fugiat. Dolore dolore est minim excepteur dolore consequat irure anim. Anim reprehenderit esse amet velit. In ut ipsum occaecat velit.',
                'done' => random_int(0,1),
            ]);
        }
    }
}

These videos helped me

qiW6LvZXxz0
SLEWYV43Jxo