Download the PHP package jfheinrich-eu/laravel-make-commands without Composer
On this page you can find all versions of the php package jfheinrich-eu/laravel-make-commands. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jfheinrich-eu/laravel-make-commands
More information about jfheinrich-eu/laravel-make-commands
Files in jfheinrich-eu/laravel-make-commands
Package laravel-make-commands
Short Description This package is aimed to be a suite of artisan commands and tools to help make the work easier.
License MIT
Homepage https://packagist.org/packages/jfheinrich-eu/laravel-make-commands
Informations about the package laravel-make-commands
Laravel Make Commands
This package is aimed to be a suite of artisan commands and tools to help make the work easier.
- Installation
- Make interface (make-commands:interface)
- Example
- Make repository (make-commands:repository)
- Usage:
- Example
- Make a service (make-commands:service)
- Example
- Data transfer object (DTO) (make-commands:dto)
- Usage
- Example
- Work with the hydration functionality
- Object Hydration
- JSON database seeder
- Usage
- Create JSON datafiles from database (make-commands:seeder-data)
- Example
- Extend Eloquent model to use views
- Example
- IDE - Helper Support for View model
- Credits
Installation
To publish the assets and config file, run following command:
This install the config file to [Project root]/app/config/make-commands.php and the stubs to [Project root]/stubs/make-commands.
To install only the config file, use this command:
To install only the stubs, use this command:
Make interface (make-commands:interface)
Creates a new interface in app\Contracts
Example
Make repository (make-commands:repository)
Creates a new repository.
Optionally you can give the model on which the repository should based on. In this case, the command creates a repository with
- a protected property for the model, which can be created by dependency injection, over the constructor.
-
it create a CRUD skeleton
-
all() : returns all records as a Eloquent collection
-
create(): creates a new record and returns then new record as Eloquent Model. The data for the record will be given by the generic
RepositoryDto
class as attribute collection. -
update(): updates a existing record and returns boolean. The data to update will be given by the generic
RepositoryDto
class as attribute collection. -
delete(): deletes a existing record and returns boolean. The deletion runs over the primary key.
- find(): returns a Eloquent model, found by primary key. Is there no record with given primary key, a ModelNotFoundException will be thrown.
-
The generic class RepositoryDto
:
The attributes property of RepositoryDto gets the required columns from the model as illuminate\Support\Collection.
This collection can be constructed like this:
Usage:
Example
app/Repositories/UserRepository.php
Make a service (make-commands:service)
Creates a new service class, which can optionally implement a existing interface and can based on a existing repository.
Example
Use existing interface App\Contracts\UserPostInterface
app/Services/UserPostService.php
Data transfer object (DTO) (make-commands:dto)
Based on the great work of Steve McDougall with his package laraval-data-object-tools.
I extend this package with implementation of „JsonSerializable“.
Usage
To generate a new DTO all you need to do is run the following artisan command:
This will generate the following class: app/Dto/MyDto.php
. By default this class
will be a final
class that implements a DtoContract
, which extends JsonSerializable
, which enforces two methods
toArray
so that you can easily cast your DTOs to arraysJsonSerialize
so that you can easily serialize your DTOs
If you are using PHP 8.2 however, you will by default get a readonly
class generated, so that you do not have
to declare each property as readonly.
Example
app/Dto/MyDto.php
Work with the hydration functionality
To work with the hydration functionality you can either use Laravels DI container, or the ready made facade.
Using the container:
To work with the facade, you can do something very similar:
Object Hydration
Under the hood this package uses an EventSauce package, created by Frank de Jonge. It is possibly the best package I have found to hydrate objects nicely in PHP. You can find the documentation here if you would like to see what else you are able to do with the package to suit your needs.
JSON database seeder
The database seeder in this package
- DatabaseJsonSeeder
- JsonSeeder
are designed to seed a table from a json data file.
Usage
All what you have to do is to
integrate DatabaseJsonSeeder
in Database\Seeder\DatabaseSeeder
After that, enter the models to be considered by the JsonSeeder into config/make-commands.php
and configure the paths to the seeder classes and the seeder data files,
below the key seeders
.
The order of the models must be specified according to the dependencies.
The JsonSeeder classes will be created automatically if they do not exist yet.
Now you need to create the data files in JSON format. The file name must match the table name of the model.
Here is an example for the User Model.
database/seeders/data/users.json
After that, you can run the seeders with
Create JSON datafiles from database (make-commands:seeder-data)
You can create the seeder JSON datafile directly from the database.
Use therefor the command
Example
This creates the files users.json
and user_posts.json
into the configured seeder data directory.
Extend Eloquent model to use views
The UseView
Trait allows to create eloquent models based on Views
, which are
- selectable
- updatable
- insertable
are.
Eloquent models must exist for the underlying tables of the View
.
The Model
must extend ViewModel
and must define at least the three attributes:
- protected $table = 'Name of the view'
- protected $mainTable = 'Table that serves as the main table'.
- protected $baseTables = ['All underlying tables']
The ViewModel
adds the following properties and methods to the Model
.
- static method create(array<string,mixed> $attributes): ViewModel|Collection<int, ViewModel>
- public function insert(array<string,mixed>|array<int,array<string,mixed>> $values): bool
- public function update(array<string,mixed> $attributes, array<string,mixed> $options): bool
- public function delete(): bool|null
- public function truncateView(): void
- public function getModelByTableName(string $table): Model
- public function getMainTable(): string
- public function getBaseTables(): string[]
- public function getMainTableAttributes(): string[]
- public function getAllTableAttributes(): array<string,string[]>
- Attribute bool is_view
Example
IDE - Helper Support for View model
Because Eloquent and Doctrine did not run back the columns of a view, two problems arise:
- Artisan model:show does not return the columns of the view
- IDE - Helper: does not recognize the columns either and thus does not generate annotations for the columns.
For the second point there is the artisan
command make-commands:view-model-hook
in this package which creates a hook class in the directory app/Support/IdeHelper. This must then only be entered in the IDE-Helper config file under hooks.
Credits
All versions of laravel-make-commands with dependencies
laravel/framework Version 10.48.22
monolog/monolog Version 3.7.0
spatie/laravel-package-tools Version 1.16.4