Tuesday, October 19, 2021

kShop docker project installer commands

 

kShop project has to three folders

1.    Project (App)

2.    kshop-measurement-api (Api)(python)

3.    kShop (php) this file save the mesurment in database

  1. Project (App)

      The app which will use the API for mesurment.

  2. kshop-measurement-api

    This file holds the measurement calculation code which is api based.

kshop-measurement-api is made fully dockerised. So to run this api project your system must need docker setup.

To run this API go to kshop-measurement-api folder and open wsl command prompt if you are in windows OS.

Make Docker image and run project in single command:

                 docker-compose up -d --build

After this command kshop-measurement-api will serve in

http://localhost:5000/ 

Later command which you may needs after docker installation:

§  To see all docker images press: docker images

§  To see all containers : docker container ls

§  To stop all Docker containers, simply run the following command in your terminal::

1.     docker kill $(docker ps -q)

2.     docker rm $(docker ps -a -q)

3.     docker rmi $(docker images -q)

 

3. kShop

This project is fully made up with php for storing measurement data which was sended by python kshop-measurement-api 

Place this project in htdocs or www folder so that kshop-measurement-api  can use this. 

Note: kshop-measurement-api  project file __init__.py line 212  res = requests.post('http://192.168.1.198/kshop/api/measurement/create_measurement.php', data=data)

You needs to modify your localhosthost IP. (E.g. http://192.168.1.198)

 

This project has a database name kshop. So to use this project import the database also.

 

             kShop Api Test from postman

url: http://localhost:5000/upload

set : header as below
x-api-key:eiWee8ep9due4deeshoa8Peichai8Eih

 

set: body params as like below

image1: file.jpg

image2:file2.jpg

imga3:file3.jpg

customer_id:11

customer_gender:male

customer_age:22

customer_height:164

customer_weight:86

Thursday, October 14, 2021

Install Docker Desktop on Windows and Linux

 

For Windows System requirements:

1.     Enable wsl2 feature in your windows.

2.     Enable virtualization to you system. (Docker desktop needs this feature)

3.     Download and install Docker Desktop

 

1. Enable wsl2 feature

WSL 2 (Windows subsystem for Linux) backend because Docker must run in Linux environment for this you have to enable wsl2 in your windows.

Enable the WSL 2 feature on Windows. For detailed instructions, refer to the (https://docs.microsoft.com/en-us/windows/wsl/install).

The following hardware prerequisites are required to successfully run WSL 2 on Windows 10 or Windows 11:

·         For Windows 11 64-bit: Home or Pro version 21H2 or higher, or Enterprise or Education version 21H2 or higher.

·         For Windows 10 64-bit: Home or Pro 2004 (build 19041) or higher, or Enterprise or Education 1909 (build 18363) or higher.

·         4GB system RAM

After you successfully install and setup wsl2 you will get wsl command prompt as like windows command prompt form search menu or any other place. With this terminal you can place Linux command in windows.

 

2. Enable virtualization

BIOS-level hardware virtualization support must be enabled in the BIOS settings. For detail info read this info: https://docs.docker.com/desktop/windows/troubleshoot/#virtualization-must-be-enabled

 

 

If your bios level virtualization is turned off then you have to turn it on.

To see virtualization is enable or not go to task manager by pressing ctrl+shif+esc button then go to performance tab.

See this article: https://mashtips.com/enable-virtualization-windows-10/

 

3. Download and install Docker Desktop

Download from here: (https://docs.docker.com/desktop/windows/install/)


                                                     For Linux

Click here to see details

Wednesday, October 6, 2021

vs-code configaration settings

 {

  "editor.fontSize": 17,

  "better-comments.multilineComments": true,

  "better-comments.highlightPlainText": false,

  "yaml.schemas": {

    "file:///c%3A/Users/Mehedi/.vscode/extensions/atlassian.atlascode-2.8.3/resources/schemas/pipelines-schema.json": "bitbucket-pipelines.yml"

  },

  "atlascode.bitbucket.enabled": false,

  "javascript.implicitProjectConfig.checkJs": false,

  "javascript.implicitProjectConfig.experimentalDecorators": false,

  // "files.autoSave": "afterDelay",

  "workbench.sideBar.location": "left",

  "window.zoomLevel": 1,

  "sync.autoDownload": true,

  "sync.autoUpload": true,

  "sync.gist": "a315796d6cea0099a0339668499857a8",

  "git.enableSmartCommit": true,

  "phpfmt.psr1": false,

  // "phpfmt.exclude": [

  //   "AllmanStyleBraces"

  // ],

  "phpfmt.passes": [

    "AlignDoubleArrow",

    "AlignEquals",

    "AlignDoubleSlashComments",

    "AlignGroupDoubleArrow",

    "AlignPHPCode",

    "AutoSemicolon",

    "ShortArray"

  ],

  "editor.formatOnSave": false,

  "[php]": {

    "editor.defaultFormatter": "kokororin.vscode-phpfmt",

    "editor.formatOnSave": true

  },

  "phpfmt.enable_auto_align": true,

  "workbench.iconTheme": "vscode-icons",

  "highlight-matching-tag.styles": {

    "opening": {

      "name": {

        "underline": "yellow"

      },

      "left": {

        "custom": {

          "borderWidth": "0 0 0 1px",

          "borderStyle": "dotted",

          "borderColor": "yellow",

          "borderRadius": "5px",

          "overviewRulerColor": "white"

        }

      },

      "right": {

        "custom": {

          "borderWidth": "0 1px 0 0",

          "borderStyle": "dotted",

          "borderColor": "yellow",

          "borderRadius": "5px",

          "overviewRulerColor": "white"

        }

      },

    }

  },

  "highlight-matching-tag.noDefaultEmptyElements": true,

  "[vue]": {

    "editor.defaultFormatter": "esbenp.prettier-vscode",

    "editor.formatOnSave": true

  },

  "[javascript]": {

    "editor.formatOnSave": true,

    "editor.defaultFormatter": "vscode.typescript-language-features",

  },

  "vetur.format.defaultFormatter.js": "vscode-typescript",

  "vetur.format.options.tabSize": 4,

  "workbench.colorTheme": "Monokai Night",

  "vsicons.dontShowNewVersionMessage": true,

  "tabnine.experimentalAutoImports": true,

  "workbench.editor.untitled.hint": "hidden",

  "files.exclude": {

    "": true

  },

  "search.useIgnoreFiles": false,

}

Tuesday, September 28, 2021

laravel 8 data_fill

 $data = [

    'products' => [

        ['name' => 'Desk 1', 'price' => 100],

        ['name' => 'Desk 2'],

    ],

];


data_fill($data, 'products.*.price', 200);


/*

    [

        'products' => [

            ['name' => 'Desk 1', 'price' => 100],

            ['name' => 'Desk 2', 'price' => 200],

        ],

    ]

*/

laravel difference between fill and update

<?php

$user = User::find(1);

// This will update immediately

$user->update(['first_name' => 'Braj', 'last_name' => 'Mohan']);


//But what if you do not want to update immediately. Suppose you also want to make user active before the actual update. Then fill method comes handy.

$user = User::find(1);

// This will not update underlying data store immediately

$user->fill(['first_name' => 'Braj', 'last_name' => 'Mohan']);

// At this point user object is still only in memory with updated values but actual update query is not performed.

// so we can have more logic here 

$user->is_active = true;

// Then finally we can save it.

$user->save(); // This will also make user active


//Update method is dumb and makes the query to database even if no values are changed. But save method is intelligent and calculates if you really changed anything and do not perform query if nothing has changed for example.


$user = User::find(1);

//suppose user object has following values after fetching

// first_name = 'Braj';

// second_name = 'Mohan';

// is_active = true;

// Then if you set the values like following without any change

$user->is_active = true; // it is already true so no change

$user->save(); // this won't perform any database query hence is efficient

Sunday, September 19, 2021

Laravel Multiple Database Connections Example

Hello Artisan

In this tutorial we will discuss about laravel multiple database connections. Do you know how to connect laravel multiple database connections? If you don't know then you are right place. In this tutorial i will show you step by step that multiple db connection in laravel 7/6.

You know that sometime we need to setup multiple database connection like mysql, mongodb etc. i can say when you work with large amount of project then you will need maybe. So let's follow bellow step to learn multiple database connection in laravel 7/6.

I will add configuration variable on .env file and use it to database configuration file. You can just follow this laravel 7/6 multiple database connections tutorial, i will also teach how to work with migration, model and database query for multiple database connection.

You have to set configuration variable on .env file. do it.

.env

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

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=mydatabase2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=root
PHP

 

Now, as i created variable in .env file, we need to use that variable on config/database.php file so let's open database.php file and add new connections key as like bellow:

config/database.php

use Illuminate\Support\Str;
  
return [
   
    'default' => env('DB_CONNECTION', 'mysql'),
   
    'connections' => [
        .....
   
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
        'mysql2' => [
            'driver' => env('DB_CONNECTION_SECOND'),
            'host' => env('DB_HOST_SECOND', '127.0.0.1'),
            'port' => env('DB_PORT_SECOND', '3306'),
            'database' => env('DB_DATABASE_SECOND', 'forge'),
            'username' => env('DB_USERNAME_SECOND', 'forge'),
            'password' => env('DB_PASSWORD_SECOND', ''),
            'unix_socket' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],
PHP

 

Here, i will give you simple example of how we can use as multiple connection to create migration. So to create migration just add which connection are you going to use.

public function up()
{
    Schema::connection('mysql2')->create('blog', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('body')->nullable();
        $table->timestamps();
    });
}
PHP

 

Now we need to setup every model as like below before working with database.

namespace App;
  
use Illuminate\Database\Eloquent\Model;
   
class Blog extends Model
{
    protected $connection = 'mysql2';
}
PHP

 

Now we have to user controller method as like below

class BlogController extends BaseController
{
    public function getRecord()
    {
        $blogModel = new Blog;
        $blogModel->setConnection('mysql2');
        $find = $blogModel->find(1);
        return $find;
    }
}
PHP

 

If you need to use query builder, then just follow and use this method.

$blogs = DB::table("blog")->get();
print_r($blogs);
   
$blogs = DB::connection('mysql2')->table("blog")->get(); 

print_r($blogs);