Skip to content

Fix for a couple of dprecations in PHP 8.4 #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 78 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
6fe022e
Resolve issue with constant names generation for camelCased table fields
Dec 11, 2018
658a141
Merge pull request #128 from VladimirBerdnik/bugfix/resolve-issue-wit…
CristianLlanos Dec 7, 2020
d4565a5
Merge branch 'v1.x' of github.com:reliese/laravel into v1.x
CristianLlanos Dec 7, 2020
8cf1d29
Roughly working relation naming improvements for BelongsTo and HasMan…
coatesap Feb 12, 2021
b8fec43
Add unit test for BelongsTo foreign key strategy
coatesap Feb 18, 2021
e42dba9
Complete unit tests for BelongsTo::name for both naming strategies
coatesap Feb 18, 2021
1195579
Tidy up BelongsToTest.php
coatesap Feb 18, 2021
718efc1
Implement working, tested HasMany relation using foreign key naming s…
coatesap Feb 18, 2021
f6eb2f8
Implement working, tested HasMany relation using related naming strategy
coatesap Feb 18, 2021
65216d0
Make sure foreign keys containing the primary key aren't incorrectly …
coatesap Feb 19, 2021
66fc897
Revert autoformatting of BelongsTo.php
coatesap Feb 19, 2021
59cea4f
Make sure HasMany can handle multi-word models
coatesap Feb 24, 2021
ac07e32
Update documentation for 'foreign_key' naming strategy
coatesap Feb 24, 2021
bdcff40
Revert autoformatting of imports
coatesap Feb 24, 2021
d6229cf
Merge pull request #192 from coatesap/improved-relationship-names
CristianLlanos Feb 25, 2021
82b39cb
Update README.md
coatesap Mar 5, 2021
2777144
Raise min PHP version to 7.3, inline with Laravel
coatesap Mar 5, 2021
9a22900
Add basic model name override functionality
coatesap Mar 5, 2021
d87cc1f
Tweak to wording
coatesap Mar 5, 2021
d0db52a
Merge pull request #195 from reliese/wording-for-seo
CristianLlanos Mar 6, 2021
6f0a710
Merge pull request #196 from reliese/override-model-names
CristianLlanos Mar 7, 2021
0292898
Include org to differentiate other model generators
coatesap Mar 23, 2021
f6b902a
Update README.md
CristianLlanos Mar 28, 2021
53ce76f
added config fillable_in_base_files
nhoemchenda Apr 20, 2021
1702342
added config fillable_in_base_files
nhoemchenda Apr 20, 2021
0519aab
Update README.md
CristianLlanos Apr 21, 2021
91de197
Merge pull request #206 from nhoemchenda/v1.x
CristianLlanos Apr 23, 2021
a8b5c39
Update Schema.php
igun997 Apr 29, 2021
96562a1
Avoid breaking same-model relationships when using base classes
coatesap Apr 29, 2021
fba5731
Merge pull request #210 from reliese/209-fix-instances-of-base-class
coatesap Apr 29, 2021
fd75015
Tweak to only skip shortening namespace when using base files
coatesap Apr 29, 2021
47eeb40
Merge pull request #211 from reliese/209-fix-instances-of-base-class
coatesap Apr 29, 2021
122a066
Fix #171 Too few arguments error when using BlameableBehavior
mvd81 May 28, 2021
56139a9
Fix #171 Too few arguments error when using BlameableBehavior
mvd81 May 28, 2021
e439995
Merge pull request #215 from mvd81/fix_too_few_arguments_error_when_u…
CristianLlanos May 30, 2021
f8f05ee
fix: add compatibility to various postgres types
nasatome Jul 1, 2021
bfceda0
fix: compatibility with identity columns in postgres
nasatome Jul 1, 2021
0f706aa
Add option to generate return type declarations for relation methods
finiteinfinity Aug 23, 2021
eeda5a5
Column type BLOB should be interpreted as string
RdeWilde Sep 15, 2021
d0430b6
Tiny typo ;)
fridzema Oct 5, 2021
a93d876
Merge pull request #221 from RdeWilde/patch-1
finiteinfinity Dec 13, 2021
0a83db1
Merge pull request #224 from fridzema/patch-1
finiteinfinity Dec 13, 2021
e016b56
Merge pull request #218 from nasatome/v1.x
CristianLlanos Feb 23, 2022
fdd4d70
Merge pull request #220 from finiteinfinity/return-types
finiteinfinity Mar 14, 2022
8df5efa
Fix Undefined index:primary for SQLITE
neatstudio Apr 15, 2022
a2f5a7d
Update Column.php
kl4ver May 5, 2022
394f1bd
Merge pull request #236 from kl4ver/patch-1
finiteinfinity May 9, 2022
a71f200
Merge pull request #235 from neatstudio/v1.x
finiteinfinity Aug 22, 2022
0db4e99
Update WhoDidIt.php
Sep 1, 2022
521f6aa
Merge pull request #242 from lugotal/v1.x
finiteinfinity Oct 14, 2022
cd266d4
Add few tables on the exclude array, Fix some typo errors
alibori Feb 23, 2023
09aadb3
Replace $dates casts with date casting through the $casts property
finiteinfinity Mar 3, 2023
5e23e16
Merge pull request #254 from finiteinfinity/dev-remote-dates-casts
finiteinfinity Mar 10, 2023
c445d0b
Replace 'date' casts with 'datetime' casts
finiteinfinity Apr 4, 2023
b283b30
Merge pull request #258 from finiteinfinity/fix-date-casts
finiteinfinity Apr 4, 2023
f8c17d9
fix(config): Clean some non default tables on except array
alibori Jul 4, 2023
3e0d2e5
Merge pull request #252 from alibori/v1.x
finiteinfinity Aug 17, 2023
def4daf
Add hidden property in BaseModels
0kyn Sep 24, 2023
8ff9498
Merge pull request #264 from 0kyn/add-hidden-in-base-files
finiteinfinity Dec 4, 2023
93cff00
Merge branch 'reliese:v1.x' into v1.x
igun997 Mar 19, 2024
d357777
fix : support laravel 11 schemas
igun997 Mar 19, 2024
4337940
Update ReferenceFactory.php
antevgen Apr 12, 2024
4438db1
Merge pull request #276 from antevgen/patch-1
finiteinfinity Apr 12, 2024
8f2313c
Use preg_replace rather than str_replace
finiteinfinity May 10, 2024
7f8f377
Merge pull request #279 from finiteinfinity/fix_property_typehints
finiteinfinity May 10, 2024
c286d28
Merge pull request #208 from igun997/v1.x
finiteinfinity Jun 6, 2024
258763b
Add support for staudenmeir/laravel-cte
finiteinfinity Aug 15, 2024
0ea3640
Merge pull request #293 from finiteinfinity/add-support-for-staudenme…
finiteinfinity Aug 15, 2024
66c01b5
fix: Solve problem with postgresql databases in Laravel 11 #286 #292
C-ArenA Aug 29, 2024
75111e5
Merge pull request #294 from C-ArenA/v1.x
finiteinfinity Sep 12, 2024
fe140be
Add support for column lists in base models
finiteinfinity Sep 12, 2024
2fc69cb
Merge pull request #295 from finiteinfinity/add_support_for_including…
finiteinfinity Sep 26, 2024
159b863
Update Factory.php
azgooon Mar 5, 2025
cea3e11
Update Column.php
azgooon Apr 2, 2025
42fccb7
Update ModelManager.php
azgooon Apr 2, 2025
d064aeb
Update Column.php
azgooon Apr 2, 2025
a2683a5
Update SchemaManager.php
azgooon Apr 2, 2025
25d6a48
Update SchemaManager.php
azgooon Apr 2, 2025
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
32 changes: 12 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Reliese Laravel
# Reliese Laravel Model Generator
[![Build Status](https://travis-ci.org/reliese/laravel.svg?branch=master)](https://travis-ci.org/reliese/laravel)
[![Latest Stable Version](https://poser.pugx.org/reliese/laravel/v/stable)](https://packagist.org/packages/reliese/laravel)
[![Total Downloads](https://poser.pugx.org/reliese/laravel/downloads)](https://packagist.org/packages/reliese/laravel)
[![Latest Unstable Version](https://poser.pugx.org/reliese/laravel/v/unstable)](https://packagist.org/packages/reliese/laravel)
[![License](https://poser.pugx.org/reliese/laravel/license)](https://packagist.org/packages/reliese/laravel)

Reliese Laravel is a collection of Laravel Components which aim is
to help the development process of Laravel applications by
providing some convenient code-generation capabilities.
Reliese Laravel Model Generator aims to speed up the development process of Laravel applications by
providing some convenient code-generation capabilities.
The tool inspects your database structure, including column names and foreign keys, in order
to automatically generate Models that have correctly typed properties, along with any relationships to other Models.

## How does it work?

Expand All @@ -22,28 +23,19 @@ It is recommended that this package should only be used on a local environment f
composer require reliese/laravel --dev
```

Then you'll need to register the provider in `app/Providers/AppServiceProvider.php` file.

```php
public function register()
{
if ($this->app->environment() == 'local') {
$this->app->register(\Reliese\Coders\CodersServiceProvider::class);
}
}
```

## Models

![Generating models with artisan](https://cdn-images-1.medium.com/max/800/1*hOa2QxORE2zyO_-ZqJ40sA.png "Making artisan code my Eloquent models")

Add the `models.php` configuration file to your `config` directory and clear the config cache:

```shell
php artisan vendor:publish --tag=reliese-models

# Let's refresh our config cache just in case
php artisan config:clear
```

## Models

![Generating models with artisan](https://cdn-images-1.medium.com/max/800/1*hOa2QxORE2zyO_-ZqJ40sA.png "Making artisan code my Eloquent models")

### Usage

Assuming you have already configured your database, you are now all set to go.
Expand Down Expand Up @@ -82,7 +74,7 @@ fit your database needs. [Check it out](https://github.com/reliese/laravel/blob/
#### 1. Keeping model changes

You may want to generate your models as often as you change your database. In order
not to lose you own model changes, you should set `base_files` to `true` in your `config/models.php`.
not to lose your own model changes, you should set `base_files` to `true` in your `config/models.php`.

When you enable this feature your models will inherit their base configurations from
base models. You should avoid adding code to your base models, since you
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=5.6.4",
"php": "^7.3|^8.0",
"doctrine/dbal": ">=2.5",
"illuminate/support": ">=5.1",
"illuminate/database": ">=5.1",
Expand Down
94 changes: 84 additions & 10 deletions config/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
| TRUE: Schema name will be prepended on the table
| FALSE:Table name will be set without schema name.
| NULL: Table name will follow laravel pattern,
| i.e if class name(plural) matches table name, then table name will not be added
| i.e. if class name(plural) matches table name, then table name will not be added
*/

'qualified_tables' => false,
Expand Down Expand Up @@ -253,11 +253,11 @@
| Casts
|--------------------------------------------------------------------------
|
| You may want to specify which of your table fields should be casted as
| something different than a string. For instance, you may want a
| text field be casted as an array or and object.
| You may want to specify which of your table fields should be cast as
| something other than a string. For instance, you may want a
| text field be cast as an array or and object.
|
| You may define column patterns which will be casted using the value
| You may define column patterns which will be cast using the value
| assigned. We have defined some fields for you. Feel free to
| modify them to fit your needs.
|
Expand All @@ -281,6 +281,10 @@

'except' => [
'migrations',
'failed_jobs',
'password_resets',
'personal_access_tokens',
'password_reset_tokens',
],

/*
Expand Down Expand Up @@ -321,6 +325,26 @@

'lower_table_name_first' => false,

/*
|--------------------------------------------------------------------------
| Model Names
|--------------------------------------------------------------------------
|
| By default the generator will create models with names that match your tables.
| However, if you wish to manually override the naming, you can specify a mapping
| here between table and model names.
|
| Example:
| A table called 'billing_invoices' will generate a model called `BillingInvoice`,
| but you'd prefer it to generate a model called 'Invoice'. Therefore, you'd add
| the following array key and value:
| 'billing_invoices' => 'Invoice',
*/

'model_names' => [

],

/*
|--------------------------------------------------------------------------
| Relation Name Strategy
Expand All @@ -333,14 +357,19 @@
generates Post::user() and User::posts()
|
| 'foreign_key' Use the foreign key as the relation name.
| (post.author --> user.id)
| generates Post::author() and User::posts_author()
| Column id's are ignored.
| This can help to provide more meaningful relationship names, and avoids naming conflicts
| if you have more than one relationship between two tables.
| (post.author_id --> user.id)
| generates Post::author() and User::posts_where_author()
| (post.editor_id --> user.id)
| generates Post::editor() and User::posts_where_editor()
| ID suffixes can be omitted from foreign keys.
| (post.author --> user.id)
| (post.editor --> user.id)
| generates the same as above.
| When the foreign key is redundant, it is omited.
| Where the foreign key matches the related table name, it behaves as per the 'related' strategy.
| (post.user_id --> user.id)
| generates User::posts() and not User::posts_user()
| generates Post::user() and User::posts()
*/

'relation_name_strategy' => 'related',
Expand All @@ -366,6 +395,19 @@
*/
'with_property_constants' => false,

/*
|--------------------------------------------------------------------------
| Optionally includes a full list of columns in the base generated models,
| which can be used to avoid making calls like
|
| ...
| \Illuminate\Support\Facades\Schema::getColumnListing
| ...
|
| which can be slow, especially for large tables.
*/
'with_column_list' => false,

/*
|--------------------------------------------------------------------------
| Disable Pluralization Name
Expand All @@ -387,6 +429,38 @@
'override_pluralize_for' => [

],

/*
|--------------------------------------------------------------------------
| Move $hidden property to base files
|--------------------------------------------------------------------------
| When base_files is true you can set hidden_in_base_files to true
| if you want the $hidden to be generated in base files
|
*/
'hidden_in_base_files' => false,

/*
|--------------------------------------------------------------------------
| Move $fillable property to base files
|--------------------------------------------------------------------------
| When base_files is true you can set fillable_in_base_files to true
| if you want the $fillable to be generated in base files
|
*/
'fillable_in_base_files' => false,

/*
|--------------------------------------------------------------------------
| Generate return types for relation methods.
|--------------------------------------------------------------------------
| When enable_return_types is set to true, return type declarations are added
| to all generated relation methods for your models.
|
| NOTE: This requires PHP 7.0 or later.
|
*/
'enable_return_types' => false,
],

/*
Expand Down
50 changes: 35 additions & 15 deletions src/Coders/Model/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace Reliese\Coders\Model;

use Illuminate\Database\DatabaseManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Reliese\Meta\Blueprint;
use Reliese\Support\Classify;
use Reliese\Meta\SchemaManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Database\DatabaseManager;
use Reliese\Support\Classify;

class Factory
{
Expand Down Expand Up @@ -319,6 +319,15 @@ private function shortenAndExtractImportableDependencies(&$placeholder, $model)
$namespacePieces = explode('\\', $usedClass);
$className = array_pop($namespacePieces);

/**
* Avoid breaking same-model relationships when using base classes
*
* @see https://github.com/reliese/laravel/issues/209
*/
if ($model->usesBaseFiles() && $usedClass === $model->getQualifiedUserClassName()) {
continue;
}

//When same class name but different namespace, skip it.
if (
$className == $model->getClassName() &&
Expand All @@ -328,7 +337,7 @@ private function shortenAndExtractImportableDependencies(&$placeholder, $model)
}

$importableDependencies[trim($usedClass, '\\')] = true;
$placeholder = str_replace($usedClass, $className, $placeholder);
$placeholder = preg_replace('!'.addslashes($usedClass).'\b!', addslashes($className), $placeholder, 1);
}
}

Expand Down Expand Up @@ -401,7 +410,8 @@ protected function body(Model $model)
$properties = array_diff($properties, $excludedConstants);

foreach ($properties as $property) {
$body .= $this->class->constant(strtoupper($property), $property);
$constantName = Str::upper(Str::snake($property));
$body .= $this->class->constant($constantName, $property);
}
}

Expand Down Expand Up @@ -445,19 +455,22 @@ protected function body(Model $model)
$body .= $this->class->field('snakeAttributes', false, ['visibility' => 'public static']);
}

if ($model->hasCasts()) {
$body .= $this->class->field('casts', $model->getCasts(), ['before' => "\n"]);
if ($model->usesColumnList()) {
$properties = array_keys($model->getProperties());

$body .= "\n";
$body .= $this->class->field('columns', $properties);
}

if ($model->hasDates()) {
$body .= $this->class->field('dates', $model->getDates(), ['before' => "\n"]);
if ($model->hasCasts()) {
$body .= $this->class->field('casts', $model->getCasts(), ['before' => "\n"]);
}

if ($model->hasHidden() && $model->doesNotUseBaseFiles()) {
if ($model->hasHidden() && ($model->doesNotUseBaseFiles() || $model->hiddenInBaseFiles())) {
$body .= $this->class->field('hidden', $model->getHidden(), ['before' => "\n"]);
}

if ($model->hasFillable() && $model->doesNotUseBaseFiles()) {
if ($model->hasFillable() && ($model->doesNotUseBaseFiles() || $model->fillableInBaseFiles())) {
$body .= $this->class->field('fillable', $model->getFillable(), ['before' => "\n"]);
}

Expand All @@ -470,7 +483,14 @@ protected function body(Model $model)
}

foreach ($model->getRelations() as $constraint) {
$body .= $this->class->method($constraint->name(), $constraint->body(), ['before' => "\n"]);
$body .= $this->class->method(
$constraint->name(),
$constraint->body(),
[
'before' => "\n",
'returnType' => $model->definesReturnTypes() ? $constraint->returnType() : null,
]
);
}

// Make sure there not undesired line breaks
Expand Down Expand Up @@ -562,11 +582,11 @@ protected function userFileBody(Model $model)
{
$body = '';

if ($model->hasHidden()) {
if ($model->hasHidden() && !$model->hiddenInBaseFiles()) {
$body .= $this->class->field('hidden', $model->getHidden());
}

if ($model->hasFillable()) {
if ($model->hasFillable() && !$model->fillableInBaseFiles()) {
$body .= $this->class->field('fillable', $model->getFillable(), ['before' => "\n"]);
}

Expand All @@ -583,7 +603,7 @@ protected function userFileBody(Model $model)
*
* @return mixed|\Reliese\Coders\Model\Config
*/
public function config(Blueprint $blueprint = null, $key = null, $default = null)
public function config(?Blueprint $blueprint = null, $key = null, $default = null)
{
if (is_null($blueprint)) {
return $this->config;
Expand Down
Loading