Saturday 25 November 2017

Laravel Migrations Command Lines

I don't trust my memory well so I'll document some migrations command

php artisan make:migration add_paid_to_users --table="users"
This line will create a book table migrations

php artisan make:migration create_books_table --create="books"




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

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('books');
    }
}

This line will add a migration script to add author_id

php artisan make:migration add_author_id_to_book_table --table="books"


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

class AddAuthorIdToBookTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->integer('author_id');
        });
    }

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



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


class DifferentArtisanTypeForMySql extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('my_table1', function (Blueprint $table) {
            $table->increments('id');

            $table->string('string_col');
            $table->string('string_col_with_length', 500);


            //create unsigned field which is the same type for field with increment attribute
            $table->unsignedInteger('unsigned_int_index')->index();
            //create string token with unique index
            $table->string('token', 100)->unique();


            $table->enum('enum_field', ['Beauty', 'Business service', 'Childrens products'])->nullable();


            $table->timestamp('timestamp_field')->nullable();
            $table->date('date_field')->nullable();
            $table->boolean('boolean_field')->default(0);

            //will create integer field with 11 in length
            $table->integer('integer_field')->default(0);

            //will create integer field with 10 in length
            $table->integer('unsigned_integer_field')->unsigned();
            
            $table->decimal('decimal_field', 6, 1)->nullable()->default(0);
            $table->tinyInteger('tinyint_field');
            $table->smallInteger('smallinteger_field')->nullable();

            $table->char('char_field', 2)->default('');
            $table->text('text_field');


        });



       // DB::statement('ALTER TABLE campaign_influencer_posts CHANGE COLUMN social_id account_id INT(11) NOT NULL ;');



        // Create table non auto increment field with index field
        Schema::create('my_table2', function ($table) {
            $table->integer('id')->unsigned()->index();
            $table->string('capital', 255)->nullable();
            $table->string('citizenship', 255)->nullable();

            $table->primary('id');
        });


        //Do you want to add a new records right away?
        Schema::create('my_table3', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->tinyInteger('active')->index();
        });

        DB::table('my_table3')->insert([
            'name' => 'Instagram',
            'active' => 1
        ]);

        DB::table('my_table3')->insert([
            'name' => 'Facebook',
            'active' => 1
        ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::table('my_table1', function (Blueprint $table) {
            $table->dropColumn('string_col');
        });

       // DB::statement('ALTER TABLE campaign_influencer_posts CHANGE COLUMN account_id social_id INT(11) NOT NULL ;');


        Schema::dropIfExists('my_table1');
        Schema::dropIfExists('my_table2');
        Schema::dropIfExists('my_table3');    
    }

    
    //You might wish to rename some fields
    // but for laravel 5.X 
    // you need to run composer require doctrine/dbal

    public function up()
    {
        Schema::table('demographics', function (Blueprint $table) {
            $table->renameColumn('social_id', 'account_id');
        });

    }
   
}



Template for migration with foreign key


    public function up(): void
    {
        Schema::table('child_table', function (Blueprint $table) {
            $table->integer('foreign_column', false, true)->nullable()->after('column_name');
            $table->foreign('foreign_column')->references('id')->on('parent_table');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('child_table', function (Blueprint $table) {
            $table->dropForeign('foreign_key_here');
            $table->dropColumn('foreign_column');
        });
    }
Here are the list of data type columns that can be added https://laravel.com/docs/5.5/migrations#creating-columns
Share:

Wednesday 8 November 2017

How to get the URL of the current page in C#

Lot of times I checked the URL for doing coding.   To get all the possible values put these codes


Share:

Monday 6 November 2017

Using SQLite in Laravel Applications

 I have been playing with Laravel and MySQL just recently and I have been enjoying using it on one of my recent project.    It's an eCommerce system done with Laravel 5.3 with Paypal and Stripe Payment.  I have implemented a authorize and capture mechanism.   

I've been using MySQL for database which works as expected but my problem is I'm using 3 machines for my development.   I don't want to use any cloud database like AWS MySQL or Google cloud db, because it's a paid one and apparently I'm not online always.   Things like I can work even in train (really hardworking right?) wherein internet is superslow.

Now comes SQLite.   SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process. SQLite reads and writes directly to ordinary disk files. A complete SQL database with multiple tables, indices, triggers, and views, is contained in a single disk file.  Meaning a portable database which will solve my dillema.


To use SQLite on Laravel

Create a database laravel.sqlite file in the database folder of your laravel project.   You might need to download SQLite browser in http://sqlitebrowser.org/

/your-project/database/laravel.sqlite



Open your database.php file in the config folder of your project and make sure what you see in the image below is the same in your project.





'default' => env('DB_CONNECTION', 'sqlite'),


Go to your .env file and and change your DB_CONNECTION to
'sqlite'. Another thing you have to do is change DB_DATABASE to the path of your laravel.sqlite on your local computer.  You can leave the port, username and password.




DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=D://github/admin-dash/database/laravel.sqlite
DB_USERNAME=root
DB_PASSWORD=
Share:

Sunday 5 November 2017

Laravel Default Env File Content

It is often helpful to have different configuration values based on
the environment where the application is running. For example, you
may wish to use a different cache driver locally than you do on your
production server.

To make this a cinch, Laravel utilizes the DotEnv PHP library by Vance
Lucas. In a fresh Laravel installation, the root directory of your application
will contain a .env.example file. If you install Laravel via Composer, this
file will automatically be renamed to .env. Otherwise, you should rename the
file manually.

Your .env file should not be committed to your application's source control,
since each developer / server using your application could require a different
environment configuration. Furthermore, this would be a security risk in the event
an intruder gain access to your source control repository, since any sensitive
credentials would get exposed.

If you are developing with a team, you may wish to continue including a .env.example
file with your application. By putting place-holder values in the example configuration
file, other developers on your team can clearly see which environment variables are
needed to run your application. You may also create a .env.testing file. This file will
override values from the .env file when running PHPUnit tests or executing Artisan commands
with the --env=testing option.



APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

.env for Laravel 8

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:9X1TB/g2Rx85u+8z+Dtm4FdSFX01BsOGs3fEJhJlrXU=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

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

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

To add paypal or stripe you can check Laravel eCommerce with Paypal and Stripe Payment
Share:

Friday 3 November 2017

Solr Sample Code and Articles

https://lucene.apache.org/solr/4_0_0/tutorial.html
https://github.com/pkainulainen/spring-data-solr-examples
https://www.petrikainulainen.net/programming/solr/spring-data-solr-tutorial-configuration/
http://nerdcenter.de/solr-multicore-installation-configuration/
http://www.params.me/2012/11/sample-solr-14-application-code-in-java.html
https://github.com/paramsethi/solr-setup
http://cmusphinx.sourceforge.net/2012/06/building-a-java-application-with-apache-nutch-and-solr/
http://www.solrtutorial.com/solrj-tutorial.html
https://www.drupal.org/node/484800
https://www.codeenigma.com/host/faq/how-do-i-create-solr-core-my-server
Share:

Popular Posts

Recent Posts

Pages

Powered by Blogger.

About Me

My photo
For the past 10 years, I've been playing with codes using PHP, Java, Rails. I do this for a living and love new things to learn and the challenges that comes with it. Besides programming I love spending time with friends and family and can often be found together catching the latest movie or planning a trip to someplace I've never been before.