Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/test-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ jobs:
strategy:
fail-fast: false
matrix:
laravel: [12.*, 11.*, 10.*, 9.*, 8.*, 7.*, 6.*]
laravel: [13.*, 12.*, 11.*, 10.*, 9.*, 8.*, 7.*, 6.*]

include:
- laravel: 13.*
testbench: 11.*
php: 8.4

- laravel: 12.*
testbench: 10.*
php: 8.4
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to `model-required-fields` will be documented in this file.

## 3.2.2 - 2026-06-18

### What's Changed

* Support laravel 13

**Full Changelog**: https://github.com/WatheqAlshowaiter/model-fields/compare/3.2.1...3.2.2

## 3.2.1 - 2025-11-08

### What's Changed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[![StandWithPalestine][ico-palestine]][link-palestine]
[![ko-fi][ico-ko-fi]][link-ko-fi]

[ico-laravel]: https://img.shields.io/badge/Laravel-%E2%89%A56.0-ff2d20?style=flat-square&logo=laravel
[ico-laravel]: https://img.shields.io/badge/Laravel-6.0%20--%2013.0-ff2d20?style=flat-square&logo=laravel
[ico-php]: https://img.shields.io/packagist/php-v/watheqalshowaiter/model-fields?color=%238892BF&style=flat-square&logo=php
[ico-version]: https://img.shields.io/packagist/v/watheqalshowaiter/model-fields.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/watheqalshowaiter/model-fields.svg?style=flat-square&color=%23007ec6
Expand Down Expand Up @@ -430,7 +430,7 @@ So Briefly, This package is useful if:

## Features

✅ Supports Laravel versions: 12, 11, 10, 9, 8, 7, and 6.
✅ Supports Laravel versions: 13, 12, 11, 10, 9, 8, 7, and 6.

✅ Supports PHP versions: 8.4, 8.3, 8.2, 8.1, 8.0, and 7.4.

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
],
"require": {
"php": ">=7.4",
"illuminate/contracts": "^12.0||^11.0||^10.0||^9.0||^8.0||^7.0||^6.0||^5.0"
"illuminate/contracts": "^13.0||^12.0||^11.0||^10.0||^9.0||^8.0||^7.0||^6.0||^5.0"
},
"require-dev": {
"nunomaduro/collision": "^8.1.1||^7.10.0||^6.0||^5.0||^4.0||^3.2",
"orchestra/testbench": "^10.0||^9.0.0||^8.22.0||^7.0||^6.0||^5.0||^4.0"
"orchestra/testbench": "^11.0||^10.0||^9.0.0||^8.22.0||^7.0||^6.0||^5.0||^4.0"
},
"replace": {
"watheqalshowaiter/model-requierd-fields": "*"
Expand Down
4 changes: 1 addition & 3 deletions src/FieldsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public function requiredFields()
$observerDefaultAttributes = Helpers::getObserverFilledFields($this->modelClass);

$primaryIndex = $this->primaryField();

$table = Helpers::getTableFromThisModel($this->modelClass);

return collect(Schema::getColumns($table))
Expand Down Expand Up @@ -259,8 +258,7 @@ public function applicationDefaultFields()
{
$this->throwIfNotUsingModelMethodFirst();

$modelInstance = new $this->modelClass;
$attributes = collect($modelInstance->getAttributes())->filter()->keys()->toArray(); // ignore null values
$attributes = collect(Helpers::getModelAttributes($this->modelClass))->filter()->keys()->toArray(); // ignore null values
$observerDefaultAttributes = Helpers::getObserverFilledFields($this->modelClass);

$allFields = $this->allFields();
Expand Down
7 changes: 3 additions & 4 deletions src/ModelFieldsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,9 @@ public function boot()
});

Builder::macro('applicationDefaultFields', function () {
$modelClass = $this->getModel();
$modelInstance = new $modelClass;
$attributes = collect($modelInstance->getAttributes())->filter()->keys()->toArray(); // ignore null values
$observerDefaultAttributes = Helpers::getObserverFilledFields($this->getModel());
$model = $this->getModel();
$attributes = collect(Helpers::getModelAttributes($model))->filter()->keys()->toArray(); // ignore null values
$observerDefaultAttributes = Helpers::getObserverFilledFields($model);

$allFields = $this->allFields();

Expand Down
36 changes: 33 additions & 3 deletions src/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use ReflectionClass;
use ReflectionException;

/**
* Here are the shared logic across multiple files, now are FieldsService & ModelFieldsServiceProvider
Expand All @@ -24,16 +26,26 @@ public static function isLaravelVersionLessThan10()
*/
public static function getModelDefaultAttributes($model)
{
return array_keys((new $model)->getAttributes());
return array_keys(static::getModelAttributes($model));
}

public static function getTableFromThisModel($model)
{
$table = (new $model)->getTable();
$table = static::getModelWithoutBooting($model)->getTable();

return str_replace('.', '__', $table);
}

/**
* @return array
*
* @throws ReflectionException
*/
public static function getModelAttributes($model)
{
return static::getModelWithoutBooting($model)->getAttributes();
}

/**
* Get fields that are automatically filled by model observers/events
* during 'creating' and 'saving' events
Expand All @@ -44,7 +56,7 @@ public static function getTableFromThisModel($model)
public static function getObserverFilledFields($modelOrClass)
{
if ($modelOrClass instanceof Model) {
$model = $modelOrClass->newInstance(); // fresh instance of same model
$model = clone $modelOrClass;
$modelClass = get_class($modelOrClass);
} else {
$model = new $modelOrClass;
Expand All @@ -63,4 +75,22 @@ public static function getObserverFilledFields($modelOrClass)

return array_keys($dirtyNoNull);
}

/**
* Get a model for passive metadata inspection without starting its boot cycle.
*
* @return object
*
* @throws ReflectionException
*/
protected static function getModelWithoutBooting($modelOrClass)
{
if ($modelOrClass instanceof Model) {
return $modelOrClass;
}

$reflection = new ReflectionClass($modelOrClass);

return $reflection->newInstanceWithoutConstructor();
}
}
19 changes: 16 additions & 3 deletions tests/Models/Uncle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ protected static function boot(): void
{
parent::boot();

self::observe(UncleObserver::class);
if (method_exists(static::class, 'whenBooted')) {
static::whenBooted(function () {
static::registerDefaultFieldEvents();
});

self::creating(function ($model) {
return;
}

static::registerDefaultFieldEvents();
}

protected static function registerDefaultFieldEvents(): void
{
static::observe(UncleObserver::class);

static::creating(function ($model) {
$model->boot_creating = 'creating';
});

self::saving(function ($model) {
static::saving(function ($model) {
$model->boot_saving = 'saving';
});
}
Expand Down
Loading