Installation
composer require b2pweb/bdf-prime
Add to config/app.php under providers:
B2PWeb\BDFPrime\PrimeServiceProvider::class,
Basic Model Definition
Define a model extending B2PWeb\BDFPrime\Model:
namespace App\Models;
use B2PWeb\BDFPrime\Model;
class User extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $fillable = ['name', 'email'];
}
First Query
$users = User::all(); // Returns collection
$user = User::find(1); // Single record
Configuration
Check config/prime.php for default settings (e.g., connection, caching). Override via:
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
],
],
CRUD Operations
// Create
$user = User::create(['name' => 'John', 'email' => 'john@example.com']);
// Update
$user->update(['name' => 'Updated Name']);
// Delete
User::destroy(1); // Single
User::where('active', false)->delete(); // Bulk
Query Building
// Chaining
$activeUsers = User::where('active', true)
->orderBy('name')
->limit(10)
->get();
// Dynamic Conditions
$query = User::query();
if ($request->has('search')) {
$query->where('name', 'like', '%'.$request->search.'%');
}
Relationships Define in model:
public function posts()
{
return $this->hasMany(Post::class);
}
Eager load:
$usersWithPosts = User::with('posts')->get();
Events & Hooks Override lifecycle methods:
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->created_at = now();
});
}
Transactions
DB::transaction(function () {
User::create(['name' => 'Alice']);
Post::create(['user_id' => 1, 'title' => 'Hello']);
});
Laravel Ecosystem
DB facade for raw queries.Prime::table() for dynamic table names:
$results = Prime::table('custom_table')->get();
API Resources Pair with Laravel’s API Resources for structured responses:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'posts_count' => $this->posts()->count(),
];
}
Testing
Use Prime::fake() for mocking:
public function test_user_creation()
{
Prime::fake();
$user = User::create(['name' => 'Test']);
$this->assertDatabaseHas('users', ['name' => 'Test']);
}
Caching
Enable query caching in config/prime.php:
'cache' => [
'enabled' => true,
'driver' => 'file',
],
Primary Key Assumptions
id; explicitly define $primaryKey if using custom keys (e.g., uuid).Mass Assignment
$fillable or $guarded to avoid mass assignment vulnerabilities.protected $guarded = ['password_hash']; // Guard specific fields
Connection Switching
User::on('mysql_secondary')->get();
config/prime.php lists all connections.Timestamps
public $timestamps = true;
const CREATED_AT = 'created_on';
const UPDATED_AT = 'updated_on';
Soft Deletes
use B2PWeb\BDFPrime\SoftDeletes;
class User extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
withTrashed() to include soft-deleted records.Query Logging
Enable in config/prime.php:
'debug' => env('APP_DEBUG', false),
Or log manually:
Prime::enableQueryLog();
$users = User::all();
dd(Prime::getQueryLog());
Common Errors
onDelete('cascade') in migrations or handle manually.$fillable).Performance
with() for eager loading.email).Custom Accessors/Mutators
public function getFullNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}
Scopes
public function scopeActive($query)
{
return $query->where('active', true);
}
Usage:
$activeUsers = User::active()->get();
Observers
class UserObserver
{
public function saving(User $user)
{
$user->updated_at = now();
}
}
Register in AppServiceProvider:
User::observe(UserObserver::class);
Macros Add global query methods:
Prime::macro('findByEmail', function ($email) {
return $this->where('email', $email)->first();
});
Usage:
$user = User::findByEmail('test@example.com');
Events Listen to model events:
User::created(function ($user) {
// Send welcome email
});
How can I help you explore Laravel packages today?