PHP code example of dayemsiddiqui / eloquent-defaults
1. Go to this page and download the library: Download dayemsiddiqui/eloquent-defaults 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/ */
dayemsiddiqui / eloquent-defaults example snippets
use Illuminate\Database\Eloquent\Model;
use dayemsiddiqui\EloquentDefaults\Traits\HasEloquentDefaults;
class Plan extends Model
{
use HasEloquentDefaults;
protected $fillable = ['company_id', 'name', 'price'];
protected static function eloquentDefaults(Company $company): array
{
return [
Plan::make(['company_id' => $company->id, 'name' => 'Basic Plan', 'price' => 999]),
Plan::make(['company_id' => $company->id, 'name' => 'Pro Plan', 'price' => 1999]),
Plan::make(['company_id' => $company->id, 'name' => 'Enterprise Plan', 'price' => 4999]),
];
}
}
$company = Company::create(['name' => 'Acme Corp']);
// Automatically creates 3 plans for this company
use dayemsiddiqui\EloquentDefaults\Traits\HasEloquentDefaults;
class Role extends Model
{
use HasEloquentDefaults;
protected $fillable = ['company_id', 'name', 'permissions'];
protected static function eloquentDefaults(Company $company): array
{
return [
Role::make(['company_id' => $company->id, 'name' => 'Admin', 'permissions' => 'all']),
Role::make(['company_id' => $company->id, 'name' => 'Editor', 'permissions' => 'read,write']),
Role::make(['company_id' => $company->id, 'name' => 'Viewer', 'permissions' => 'read']),
];
}
}
// All of these will create defaults when a Company is created
class Plan extends Model {
use HasEloquentDefaults;
protected static function eloquentDefaults(Company $company): array { /* ... */ }
}
class Role extends Model {
use HasEloquentDefaults;
protected static function eloquentDefaults(Company $company): array { /* ... */ }
}
class Setting extends Model {
use HasEloquentDefaults;
protected static function eloquentDefaults(Company $company): array { /* ... */ }
}
class Subscription extends Model
{
use HasEloquentDefaults;
protected static function eloquentDefaults(User $user): array
{
$defaults = [
Subscription::make(['user_id' => $user->id, 'plan' => 'free']),
];
// Add bonus subscription for verified users
if ($user->email_verified_at) {
$defaults[] = Subscription::make([
'user_id' => $user->id,
'plan' => 'verified_bonus',
'expires_at' => now()->addDays(30)
]);
}
return $defaults;
}
}
class Profile extends Model
{
use HasEloquentDefaults;
protected static function eloquentDefaults(User $user): array
{
return [
Profile::make([
'user_id' => $user->id,
'bio' => "Welcome {$user->name}! Thanks for joining our platform.",
'avatar' => 'default-avatar.png',
'preferences' => json_encode([
'theme' => 'light',
'notifications' => true,
'locale' => 'en'
])
]),
];
}
}
use dayemsiddiqui\EloquentDefaults\Facades\EloquentDefaults;
EloquentDefaults::debugRegistrations();
// Returns array of all target models and their provider models
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.