inisiatif/model-shared
Kumpulan model Eloquent bersama untuk Inisiatif Zakat Indonesia: pekerjaan, tingkat pendidikan, wilayah (negara–provinsi–desa), dan status perkawinan. Mendukung relasi dinamis Branch dan Employee pada model Donor via resolveRelationUsing.
Installation:
composer require inisiatif/model-shared
Publish migrations (if needed):
php artisan vendor:publish --provider="Inisiatif\ModelShared\ModelSharedServiceProvider" --tag="migrations"
Run Migrations:
php artisan migrate
First Use Case:
Use the Donor model to fetch or create donor records:
use Inisiatif\ModelShared\Models\Donor;
$donor = Donor::find(1);
$donor->degree; // Access education level
$donor->maritalStatus; // Access marital status
Dynamic Relations Setup:
Add dynamic relations in your AppServiceProvider:
public function boot()
{
Donor::resolveRelationUsing('branch', function ($model) {
return $model->belongsTo(\App\Models\Branch::class, 'branch_id');
});
}
vendor/inisiatif/model-shared/src/Models/ for all available models.vendor/inisiatif/model-shared/database/migrations/ for schema details.vendor/inisiatif/model-shared/src/ModelSharedServiceProvider.php for bootstrapping.Model Extensions: Extend shared models with custom attributes or relations:
// In AppServiceProvider
Donor::extend('customAttribute', function ($model) {
return $model->hasOne(CustomAttribute::class);
});
Geographic Hierarchy: Use nested relations for location-based queries:
$village = \Inisiatif\ModelShared\Models\Village::with(['district.province.country'])->find(1);
Dynamic Relations: Define polymorphic relations dynamically (as shown in the README) to avoid hardcoding foreign keys.
Query Scoping:
Leverage built-in scopes (e.g., activeDonors() if added in future versions):
$activeDonors = Donor::active()->get();
Migrations: Run migrations for shared tables:
php artisan migrate --path=vendor/inisiatif/model-shared/database/migrations
Donor Management:
$donor = Donor::create([
'name' => 'John Doe',
'degree_id' => \Inisiatif\ModelShared\Models\Degree::where('name', 'S1')->first()->id,
'marital_status_id' => \Inisiatif\ModelShared\Models\MaritalStatus::where('name', 'Married')->first()->id,
]);
$donor->branch()->associate(\App\Models\Branch::find(1));
$donor->save();
Geographic Data:
$village = \Inisiatif\ModelShared\Models\Village::withPath()->find(1);
$regions = \Inisiatif\ModelShared\Models\Region::search('Jakarta')->get();
Outflows and Donations:
$outflow = \Inisiatif\ModelShared\Models\Outflow::create([
'donor_id' => $donor->id,
'amount' => 1000000,
'purpose' => 'Education',
]);
Customize Migrations:
Override shared migrations in your app’s database/migrations to add custom columns:
Schema::table('donors', function (Blueprint $table) {
$table->string('custom_field')->nullable();
});
Use Traits:
Reuse traits from the package (e.g., HasDynamicRelations) in your models:
use Inisiatif\ModelShared\Traits\HasDynamicRelations;
API Endpoints:
Build API routes for shared models (e.g., /api/degrees):
Route::apiResource('degrees', \Inisiatif\ModelShared\Models\Degree::class);
Testing: Mock shared models in tests:
$this->partialMock(Donor::class, function ($mock) {
$mock->shouldReceive('degree')->andReturn(new Degree());
});
Indonesia-Specific Data:
Village, District) are Indonesia-only. Avoid using them for global projects.Village tables may not align with non-Indonesian administrative divisions.Dynamic Relations:
branch, employee) require manual setup in boot() of a service provider. Forgetting this will cause Call to undefined method errors.// ❌ Will fail if not resolved
$donor->branch; // Throws error
Migration Conflicts:
donors table, skip or modify the shared migration.UUIDs:
Partner) use UUIDs. Ensure your Laravel app supports UUIDs (e.g., laravel/breeze or webpatser/laravel-uuid).Typo Fixes:
Unmaintained Package:
Illuminate\Database changes.Dynamic Relation Performance:
with() to eager-load them:
$donor = Donor::with(['branch', 'employee'])->find(1);
Dynamic Relation Errors:
Donor::branch fails, check if the relation was resolved in boot():
// Debug: Check if relation exists
dd(Donor::relations());
Migration Errors:
table key typos).UUID Issues:
AppServiceProvider has:
use Illuminate\Database\Eloquent\Concerns\HasUuids;
class Partner extends Model
{
use HasUuids;
}
Geographic Data Mismatches:
search API (if implemented) to validate:
$regions = \Inisiatif\ModelShared\Models\Region::search('Jakarta')->get();
Extend Models Safely:
extend() to add custom attributes without modifying the package:
Donor::extend('customField', function ($model) {
return $model->hasOne(CustomField::class);
});
Customize Migrations:
// In your migration
Schema::table('donors', function (Blueprint $table) {
$table->string('custom_column')->nullable();
});
Use Traits:
HasDynamicRelations) in your models:
use Inisiatif\ModelShared\Traits\HasDynamicRelations;
class CustomDonor extends Donor
{
use HasDynamicRelations;
}
Leverage Dynamic Relations:
branch, employee) to avoid hardcoding foreign keys:
Donor::resolveRelationUsing('branch', function ($model) {
return $model->belongsTo(\App\Models\Branch::class, 'branch_id');
});
Test Thoroughly:
public function test_dynamic_relation()
{
$donor
How can I help you explore Laravel packages today?