1. Go to this page and download the library: Download codemonster-ru/database library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Codemonster\Database\Schema\Blueprint;
// You can also use Schema::forConnection($db) if you need a schema instance directly.
$db->schema()->create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->boolean('active')->default(1);
$table->timestamps();
});
$db->schema()->table('users', function (Blueprint $table) {
$table->string('avatar')->nullable();
$table->integer('age')->default(0);
});
use Codemonster\Database\Migrations\Migration;
use Codemonster\Database\Schema\Blueprint;
return new class extends Migration {
public function up(): void
{
schema()->create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
});
}
public function down(): void
{
schema()->drop('posts');
}
};
use Codemonster\Database\Seeders\Seeder;
return new class extends Seeder {
public function run(): void
{
db()->table('users')->insert([
'name' => 'Admin',
'email' => '[email protected]',
]);
}
};
use Codemonster\Database\ORM\Model;
class User extends Model
{
protected string $table = 'users';
protected array $fillable = ['name', 'email', 'password'];
protected array $hidden = ['password'];
protected array $casts = [
'created_at' => 'datetime',
];
}
class User extends Model {
public function posts() {
return $this->hasMany(Post::class);
}
}
class Post extends Model {
public function author() {
return $this->belongsTo(User::class, 'user_id');
}
}
$user->posts;
$user->load('posts');
use Codemonster\Database\Traits\SoftDeletes;
class User extends Model {
use SoftDeletes;
}