PHP code example of marwen-brini / bob-the-builder
1. Go to this page and download the library: Download marwen-brini/bob-the-builder 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/ */
marwen-brini / bob-the-builder example snippets
// Hydrate models from database without checking fillable/guarded
$model->forceFill($databaseRow); // Bypasses mass assignment protection
$builder = $connection->table('users')
->enableExistsCache(120) // Cache for 2 minutes
->where('email', '[email protected]');
// First call - hits database
$exists = $builder->exists();
// Subsequent calls within 2 minutes - uses cache
$exists = $builder->exists(); // No database query!
// Global scopes automatically apply to relationships
$user->posts()->get(); // Includes global scopes from Post model
// Or disable for specific queries
$user->posts()->withoutGlobalScopes()->get();
// Option 1: Configure via connection config
$connection = new Connection([
'driver' => 'mysql',
'database' => 'mydb',
// ... other config
'fetch' => PDO::FETCH_OBJ, // Return objects instead of arrays
]);
// Option 2: Change dynamically at runtime
$connection->setFetchMode(PDO::FETCH_OBJ); // Use objects
$users = $connection->table('users')->get(); // Returns array of stdClass objects
$connection->setFetchMode(PDO::FETCH_ASSOC); // Back to arrays (default)
$users = $connection->table('users')->get(); // Returns array of associative arrays
// Get all records
$users = $connection->table('users')->get();
// Get specific columns
$users = $connection->table('users')
->select('id', 'name', 'email')
->get();
// Get first record
$user = $connection->table('users')
->where('email', '[email protected]')
->first();
// Get single value
$email = $connection->table('users')
->where('id', 1)
->value('email');
// Basic where
$users = $connection->table('users')
->where('status', 'active')
->get();
// Multiple conditions
$users = $connection->table('users')
->where('status', 'active')
->where('age', '>', 18)
->get();
// Or where
$users = $connection->table('users')
->where('role', 'admin')
->orWhere('role', 'moderator')
->get();
// Where in
$users = $connection->table('users')
->whereIn('id', [1, 2, 3])
->get();
// Where between
$users = $connection->table('users')
->whereBetween('age', [18, 65])
->get();
// Where null
$users = $connection->table('users')
->whereNull('deleted_at')
->get();
$connection->table('users')->chunk(100, function ($users) {
foreach ($users as $user) {
// Process user
}
});
// Stream millions of rows with minimal memory usage
foreach ($connection->table('users')->cursor() as $user) {
// Process one user at a time
// Memory usage stays constant regardless of dataset size
processUser($user);
}
// Performance: 1M+ rows/second throughput
// Memory: ~8MB for 15,000 rows
// Enable query logging
$connection->enableQueryLog();
// Run queries
$users = $connection->table('users')->get();
// Get query log
$queries = $connection->getQueryLog();
print_r($queries);
// Get SQL without executing
$sql = $connection->table('users')
->where('active', true)
->toSql();
echo $sql; // select * from "users" where "active" = ?
// Get bindings
$bindings = $connection->table('users')
->where('active', true)
->getBindings();
print_r($bindings); // [true]
use Bob\Database\Model;
class User extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $fillable = ['name', 'email', 'password'];
// Define relationships
public function posts()
{
return $this->hasMany(Post::class);
}
public function profile()
{
return $this->hasOne(Profile::class);
}
public function roles()
{
return $this->belongsToMany(Role::class, 'user_roles');
}
}
class User extends Model
{
public function profile()
{
return $this->hasOne(Profile::class);
}
}
// Usage
$user = User::find(1);
$profile = $user->profile; // Automatically loads the profile
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
// Usage
$user = User::find(1);
$posts = $user->posts; // Returns a Collection of Post models
class Post extends Model
{
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
}
// Usage
$post = Post::find(1);
$author = $post->author; // Loads the User model
class User extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class, 'user_roles', 'user_id', 'role_id');
}
}
// Usage
$user = User::find(1);
$roles = $user->roles; // Returns Collection of Role models
// Attach/Detach relationships
$user->roles()->attach($roleId);
$user->roles()->detach($roleId);
$user->roles()->sync([1, 2, 3]); // Sync to exact set of IDs
// Load users with their posts and comments
$users = User::with('posts.comments')->get();
// Multiple relationships
$users = User::with(['posts', 'profile', 'roles'])->get();
// Eager loading with constraints
$users = User::with(['posts' => function($query) {
$query->where('published', true);
}])->get();
use Bob\Schema\Schema;
// Create a new table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
// Modify existing table
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable()->after('email');
$table->index('phone');
});
// Drop table
Schema::dropIfExists('users');
use Bob\Schema\Schema;
// Create WordPress-style post table
Schema::createWordPress('custom_posts', function (WordPressBlueprint $table) {
$table->wpPost(); // All standard WordPress post columns
$table->wpPostIndexes(); // Standard WordPress indexes
});
// Create custom meta table
Schema::createWordPress('custom_meta', function (WordPressBlueprint $table) {
$table->wpMeta('custom'); // Creates: meta_id, custom_id, meta_key, meta_value
});
// Create WooCommerce order table
Schema::createWordPress('wc_custom_orders', function (WordPressBlueprint $table) {
$table->wcOrder(); // All WooCommerce HPOS order columns
});
use Bob\Database\Migrations\Migration;
use Bob\Schema\Blueprint;
use Bob\Schema\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
}
use Bob\Database\Migrations\MigrationRunner;
use Bob\Database\Migrations\MigrationRepository;
use Bob\Database\Connection;
$connection = new Connection([/* config */]);
$repository = new MigrationRepository($connection, 'migrations');
$runner = new MigrationRunner($connection, $repository, ['/path/to/migrations']);
// Run pending migrations
$runner->run();
// Rollback last batch
$runner->rollback();
// Rollback all migrations
$runner->reset();
// Drop all tables and re-run migrations
$runner->fresh();
// Rollback and re-run all migrations
$runner->refresh();
// Check migration status
$status = $runner->status();
use Bob\Schema\Inspector;
$inspector = new Inspector($connection);
// Get all tables
$tables = $inspector->getTables();
// Get table structure
$columns = $inspector->getColumns('users');
$indexes = $inspector->getIndexes('users');
$foreignKeys = $inspector->getForeignKeys('users');
// Generate migration from existing table
$migration = $inspector->generateMigration('users');
file_put_contents('2024_01_01_000000_create_users_table.php', $migration);