PHP code example of watheqalshowaiter / model-fields
1. Go to this page and download the library: Download watheqalshowaiter/model-fields 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/ */
watheqalshowaiter / model-fields example snippets
Schema::create('users', function (Blueprint $table) {
$table->id(); // primary key
$table->string('name'); // e
$table->string('password'); //
// Facade way
use WatheqAlshowaiter\ModelFields\Fields;
use App\Models\User;
Fields::model(User::class)->allFields(); // returns ['id', 'name', 'email', 'email_verified_at', 'password', 'random_number', 'remember_token', 'created_at', 'updated_at']
Fields::model(User::class)->
Fields::model(Post::class)->applicationDefaultFields();
//or
Post::applicationDefaultFields();
// If there are default attributes in the model
class Post extends Model
{
protected $attributes = [
'title' => 'default title',
'description' => null, // will be ignored
];
protected $dispatchesEvents = [
// if there is a field autofilled by this event,
// then it will be added to the application default fields
'creating' => PostCreatingEvent::class,
];
// or any event-filled fields
protected static function boot(): void
{
parent::boot();
self::observe(PostObserver::class);
self::creating(function ($model) {
$model->uuid = Str::uuid();
});
self::saving(function ($model) {
$model->ulid = Str::ulid();
});
}
}
// the same in the observer class
class PostObserver
{
public function creating(Post $model): void
{
// ..
}
public function saving(Post $model): void
{
// ..
}
}
// returns
// [
// 'title', 'uuid', 'ulid',
// ]
Fields::model(Post::class)->defaultFields();
//or
Post::defaultFields();
// This will combine application and database defaults
class Post extends Model
{
protected $attributes = [
'title' => 'default title',
];
protected static function boot(): void
{
parent::boot();
self::creating(function ($model) {
$model->description = 'default description';
});
}
}
// returns
// [
// 'active', 'title', 'description',
// ]
sh
php artisan vendor:publish --provider="WatheqAlshowaiter\ModelFields\ModelFieldsServiceProvider" --tag="config"
sh
php artisan model:fields \\Modules\\Order\\src\\Models\\Order
# or
php artisan model:fields "Modules\Order\src\Models\Order"
sh
php artisan model:fields User --format=json
php artisan model:fields User --format=table
php artisan model:fields User --format=list # default
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.