Summary
1 Installation
-
2 Cosmo Commands
-
2.1 Vortex
2.1.1 vortex:install
2.1.2 vortex:server
-
2.2 Migration
2.2.1 migrate
2.2.2 migrate:rollback
2.2.3 migrate:list
2.3 Seed
-
2.4 Make
2.4.1 make:migration
2.4.2 make:factory
2.4.3 make:seed
2.4.4 make:controller
2.4.5 make:middleware
2.4.6 make:model
2.4.7 make:service
2.4.8 make:exception
-
-
3 Database
-
3.1 Migration
3.1.1 Up method
3.1.2 Table structure
3.1.3 Down method
3.1.4 Column types
3.1.5 Column constraints
3.2 Factory
3.3 Seed
-
-
4 Model
4.1 Query builder
-
5 Controller
5.1 Validation
/|\ (1) Installation
- On terminal run the composer command with your new project name
composer create-project vortex-framework/vortex YOUR_PROJECT_NAME
- Copy the .env.example and rename to .env
- Set the variables to continue the installation
APP_NAME=$PROJECT_NAME
APP_ENV=local
APP_URL=$PROJECT_URL
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=$DATABASE_NAME
DB_USERNAME=$DATABASE_USERNAME
DB_PASSWORD=$DATABASE_PASSWORD
DB_CHARSET=utf8mb4
- On terminal go to your root project directory and run Cosmo Vortex Installation command
php cosmo vortex:install
-
Your output should be equal to this
-
load environment........... SUCCESS
-
first migrations............... SUCCESS
-
set time zone................. SUCCESS
-
composer install................. SUCCESS
-
npm install................. SUCCESS
-
npm compile................. SUCCESS
-
To test your project run:
php cosmo migrate
php cosmo seed
- That create a User default table and create 20 Users
/|\ (2) Cosmo Commands
-
Cosmo is a command line interface that help you in your project development
-
Run Cosmo index command, that list for you all available commands
php cosmo
(2.1) Vortex
(2.1.1) vortex:install
- This command install all dependencies and set up your basic functionality
php cosmo vortex:install
(2.1.2) vortex:serve
- This command up a PHP server, you can set the port in
APP_LOCALHOST_SERVER_PORT
on .env
php cosmo vortex:install
(2.2) Migration
- For understand more about
migrations
look the Migrations explanation (3.1)
(2.2.1) migrate
- This command run all
Up
methods frommigrations
in App/Migrations directory - You can pass the filename of the migration to run else all not ran migrations will run
php cosmo migrate
(2.2.2) migrate:rollback
- This command run all
Down
methods frommigrations
in App/Migrations directory - You can pass the
filename
of the migration to run else all not ran migrations will run
php cosmo migrate:rollback
(2.2.2) migrate:list
- This command display all
migrations
and your status likeRUN
orRAN
- You can pass the
filename
of the migration to run else all not ran migrations will run
php cosmo migrate:rollback
(2.3) Seed
- This command run all
handle
methods fromseeds
in App/Seeds directory - You can pass the
filename
of the seed to run else all not ran migrations will run
php cosmo seed
(2.4) Make
(2.4.1) make:migration
- This command create a new Migration class in App/Migrations directory
- You need pass the classname
php cosmo make:migration $MigrationName
(2.4.2) make:factory
- This command create a new Factory class in App/Factories directory
- You need pass the classname
php cosmo make:factory $FactoryName
(2.4.3) make:seed
- This command create a new Seed class in App/Seeds directory
- You need pass the classname
php cosmo make:seed $SeedName
(2.4.4) make:controller
- This command create a new Controller class in App/Controllers directory
- You need pass the classname
php cosmo make:controller $ControllerName
(2.4.5) make:middleware
- This command create a new Seed class in App/Seeds directory
- You need pass the classname
php cosmo make:seed $SeedName
(2.4.6) make:model
- This command create a new Controller class in App/Controllers directory
- You need pass the classname
php cosmo make:controller $ControllerName
(2.4.7) make:service
- This command create a new Service class in App/Services directory
- You need pass the classname
php cosmo make:service $ServiceName
(2.4.8) make:exception
- This command create a new Exception class in App/Exceptions directory
- You need pass the classname
php cosmo make:exception $ExceptionName
/|\ (3) Database
(3.1) Migration
(3.1.1) Up method
- The Up method will be called when you run the migrate command (2.2.1)
(3.1.2) Table structure
- This is a simple example of Users table
- The Schema:create need the table name and anonymous function with the table columns like:
public static function up(): void
{
Schema::create('users', function (TableBuilder $table) {
$table->id()->unique()->notNull()->autoIncrement();
$table->varchar('name', '255')->notNull();
$table->varchar('email', 255)->unique()->notNull();
$table->varchar('password', 255)->notNull();
$table->timeStamp('created_at');
$table->timeStamp('updated_at');
return $table;
});
}
(3.1.3) Down method
- The Down method will be called when you run migrate:rollback command (2.2.2)
public static function down(): void
{
Schema::drop('users');
}
(3.1.4) Column types
COLUMN TYPES | ||
---|---|---|
BIG INT | ENUM | SMALL INT |
BINARY | FLOAT | TEXT |
BIT | INT | TIME |
BLOB | INTEGER | TIMESTAMP |
BOOL | JSON | TINY BLOB |
BOOLEAN | LONG BLOB | TINY INT |
CHAR | LONG TEXT | TINY TEXT |
DATE | MEDIUM BLOB | UUID |
DATETIME | MEDIUM INT | VAR BINARY |
DECIMAL | MEDIUM TEXT | VARCHAR |
DOUBLE | SET | YEAR |
(BIG INT)
-
Description -
Big Int
-
Parameters - string
$column_name
- Usage
$table->bigInt($column_name);
(BINARY)
-
Description -
Binary
-
Parameters - string
$column_name
- Usage
$table->binary($column_name);
(BIT)
-
Description -
Bit
-
Parameters - string
$column_name
- Usage
$table->bit($column_name);
(BLOB)
-
Description -
Blob
-
Parameters - string
$column_name
- Usage
$table->blob($column_name);
(BOOL)
-
Description -
Bool
-
Parameters - string
$column_name
- Usage
$table->bool($column_name);
(BOOLEAN)
-
Description -
Boolean
-
Parameters - string
$column_name
- Usage
$table->boolean($column_name);
(CHAR)
-
Description -
Char
-
Parameters - string
$column_name
, int$var_length
- Usage
$table->char($column_name, $var_length);
(DATE)
-
Description -
Date
-
Parameters - string
$column_name
- Usage
$table->date($column_name);
(DATE TIME)
-
Description -
Date Time
-
Parameters - string
$column_name
- Usage
$table->dateTime($column_name);
(DECIMAL)
-
Description -
Decimal
-
Parameters - string
$column_name
- Usage
$table->decimal($column_name);
(DOUBLE)
-
Description -
Double
-
Parameters - string
$column_name
- Usage
$table->double($column_name);
(ENUM)
-
Description -
Enum
-
Parameters - string
$column_name
and array$options
- Usage
$table->enum($column_name, $options);
(FLOAT)
-
Description -
Float
-
Parameters - string
$column_name
- Usage
$table->float($column_name);
(ID)
-
Description - A
Big Int
column -
Parameters - string
$column_name
with default value 'id' - Usage
$table->id($column_name);
(INT)
-
Description -
Int
-
Parameters - string
$column_name
- Usage
$table->int($column_name);
(INTEGER)
-
Description -
Integer
-
Parameters - string
$column_name
- Usage
$table->integer($column_name);
(JSON)
-
Description -
Json
-
Parameters - string
$column_name
- Usage
$table->json($column_name);
(LONG BLOB)
-
Description -
Long Blob
-
Parameters - string
$column_name
- Usage
$table->longBlob($column_name);
(LONG TEXT)
-
Description -
Long Text
-
Parameters - string
$column_name
- Usage
$table->longText($column_name);
(MEDIUM BLOB)
-
Description -
Medium Blob
-
Parameters - string
$column_name
- Usage
$table->mediumBlob($column_name);
(MEDIUM INT)
-
Description -
Medium Int
-
Parameters - string
$column_name
- Usage
$table->mediumInt($column_name);
(MEDIUM TEXT)
-
Description -
Medium Text
-
Parameters - string
$column_name
- Usage
$table->mediumText($column_name);
(SET)
-
Description -
Set
-
Parameters - string
$column_name
- Usage
$table->set($column_name);
(SMALL INT)
-
Description -
Small Int
-
Parameters - string
$column_name
- Usage
$table->smallInt($column_name);
(TEXT)
-
Description -
Text
-
Parameters - string
$column_name
- Usage
$table->text($column_name);
(TIME)
-
Description -
Time
-
Parameters - string
$column_name
- Usage
$table->time($column_name);
(TIMESTAMP)
-
Description -
Timestamp
-
Parameters - string
$column_name
- Usage
$table->timestamp($column_name);
(TINY BLOB)
-
Description -
Tiny Blob
-
Parameters - string
$column_name
- Usage
$table->tinyBlob($column_name);
(TINY INT)
-
Description -
Tiny Int
-
Parameters - string
$column_name
- Usage
$table->tinyInt($column_name);
(TINY TEXT)
-
Description -
Tiny Text
-
Parameters - string
$column_name
- Usage
$table->tinyText($column_name);
(UUID)
-
Description - A
Varchar
column -
Parameters - string
$column_name
with default value 'id' - Usage
$table->uuid($column_name);
(VAR BINARY)
-
Description -
Var Binary
-
Parameters - string
$column_name
- Usage
$table->varBinary($column_name);
(VARCHAR)
-
Description -
Varchar
-
Parameters - string
$column_name
, int$varchar_length
- Usage
$table->varchar($column_name, $varchar_length);
(YEAR)
-
Description -
Year
-
Parameters - string
$column_name
- Usage
$table->year($column_name);
(3.1.5) Column constraints
COLUMN CONSTRAINTS | ||
---|---|---|
AUTO INCREMENT | FOREIGN KEY | PRIMARY KEY |
DEFAULT | NOT NULL | UNIQUE |
(AUTO INCREMENT)
-
Description -
Auto_increment
- Usage
$table->id()->autoIncrement();
(DEFAULT)
-
Description -
Default
- Usage
$table->varchar('name', 100)->default('default_value');
(FOREIGN KEY)
-
Description -
Foreign Key
- Usage
$table->int('user_id')->forignKey('users', 'id');
(NOT NULL)
-
Description -
Not null
- Usage
$table->id()->notNull();
(PRIMARY KEY)
-
Description -
Primary key
- Usage
$table->id()->primaryKey();
(UNIQUE)
-
Description -
Unique
- Usage
$table->varchar('email', 100)->unique();
(3.2) Factory
-
The factory classes are used to mount new Model's object to insert in the database, you can use the faker to help you
-
Usage
class UserFactory extends Factory
{
public static function frame(): array
{
return [
'name' => faker()->name(),
'email' => faker()->safeEmail,
'password' => Hash::hashPassword('password'),
'created_at' => DateTime::currentDateTime(),
];
}
}
(3.3) Seed
- Use seeds for massive insert into your database.
In additional you can use your Factory class to personalize your inserts.
You need write your code into method
handler
in your Seed class.
- Run seeds with the command:
php cosmo seed
This method create only one User
class UserSeeder extends Seeder
{
public static function handler(): void
{
Schema::insert('users', [
'name' => 'Username',
'email' => 'username@vortex.com',
'password' => Hash::hashPassword('password'),
'created_at' => DateTime::currentDateTime(),
])->get();
}
}
This method enable you to set the number of inserts
class UserSeeder extends Seeder
{
public static function handler(): void
{
self::factory('users', UserFactory::class, 20);
}
}
/|\ (4) Model
- The Model represent the abstraction of the object who you need represent.
First step is set the
$table
attribute
class User extends Model
{
public string $table = 'users';
}
- From Model class you can call some methods to interact with your database, this is the list of available methods using User class for example:
CREATE
$user = new User([
'name' => $username,
'email' => $email,
'password' => $password
]);
$user->create();
DELETE
User::delete()->where('id', $user_id)->get();
FIND
$user->find(['id', 'email'])->where('name', 'A%', 'like')->get();
FIRST
$user->first();
LAST
$user->last();
UPDATE
User::update([
'name' => 'new_username',
'email' => 'new_email'
])->where('id', $user_id)->get();