Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/muzammal01/syncmodelfillable

A Laravel package that automatically syncs a model's $fillable fields with its corresponding database migration columns by uisng simple Artisan command.
https://github.com/muzammal01/syncmodelfillable

artisan database fillable laravel laravel-10 laravel-11 laravel-8 laravel-9 laravel-development laravel-package migration model package php sync

Last synced: about 1 month ago
JSON representation

A Laravel package that automatically syncs a model's $fillable fields with its corresponding database migration columns by uisng simple Artisan command.

Awesome Lists containing this project

README

        

# SyncModelFillable

[![Latest Version on Packagist](https://img.shields.io/packagist/v/muzammal/syncmodelfillable.svg?style=flat-square)](https://packagist.org/packages/muzammal/syncmodelfillable)
[![Total Downloads](https://img.shields.io/packagist/dt/muzammal/syncmodelfillable.svg?style=flat-square)](https://packagist.org/packages/muzammal/syncmodelfillable)

**SyncModelFillable** is a Laravel package designed to help automatically sync a model's `$fillable` fields with its database migration columns. πŸŽ‰ With just a simple Artisan command, you can keep your model properties up-to-date with your migration files effortlessly.

## ✨ Features

- πŸ› οΈ Syncs model `$fillable` properties with migration columns.
- πŸ“¦ Supports Laravel versions 8, 9, 10, and 11.
- βš™οΈ Customizable to exclude specific columns, like timestamps.
- πŸ”„ **New**: Added a `--ignore` flag to exclude specific models during sync.

---

## πŸš€ Installation

1. **Install the package via Composer:**

```bash
composer require muzammal/syncmodelfillable
```

2. **(Optional) Publish the configuration file:**

If you'd like to customize which columns are excluded from the `$fillable` fields, publish the configuration file:

```bash
php artisan vendor:publish --provider="Muzammal\Syncmodelfillable\SyncModelFillableServiceProvider"
```

This will create a `config/syncfillable.php` file where you can specify columns to exclude (such as `created_at`, `updated_at`, `deleted_at` etc.).

---

## πŸ“˜ Usage

This package provides an Artisan command `sync:fillable` that lets you sync a model's `$fillable` fields with its migration columns.

### Sync a Specific Model's `$fillable` Fields

To sync the `$fillable` fields of a specific model, use the command with the model name. For example, if you have a model named `Post`:

```bash
php artisan sync:fillable Post
```

This will:
- Look for the `Post` model in the `app/Models` directory.
- Find the migration file associated with the model’s database table.
- Update the `$fillable` property in the model with the columns from the migration file.

### Sync All Models in `app/Models`

To sync all models in the `app/Models` directory, use `all` as the parameter:

```bash
php artisan sync:fillable all
```

This will:
- Look for all models in the `app/Models` directory.
- Match each model with its migration file.
- Update the `$fillable` property for each model.

### **New**: Exclude Models with the `--ignore` Flag

You can now exclude specific models from the sync operation using the `--ignore` flag. For example:

```bash
php artisan sync:fillable all --ignore=User
```

This will sync all models except `User`. You can also pass multiple models to ignore:

```bash
php artisan sync:fillable all --ignore=User,Product,Order
```

If you want to run the sync for a single model, ignoring doesn't apply here:

```bash
php artisan sync:fillable Product
```

### **How It Works**
- The `--ignore` flag allows you to pass a comma-separated list of model names to exclude during the sync process.
- If a model is listed in the ignore list, it will be skipped during the sync.

---

## βš™οΈ Configuration

The configuration file `syncfillable.php` allows you to specify which columns to exclude from the `$fillable` fields. By default, common timestamp columns (`created_at`, `updated_at`, `deleted_at`) are excluded.

**Example configuration:**

```php
return [
'excluded_columns' => ['created_at', 'updated_at', 'deleted_at'],
];
```

Add any column names here that you want to exclude from the `$fillable` fields.

---

## πŸ” Example

Suppose you have a `Post` model with a migration that defines columns such as `name`, `slug`, and `content`. Running the following command:

```bash
php artisan sync:fillable Post
```

Would automatically set the `$fillable` fields in `Post.php` as follows:

```php
protected $fillable = ['name', 'slug', 'content'];
```

---

## πŸ“œ License

This package is open-source software licensed under the MIT license.