generated from open-southeners/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Getting started | ||
|
||
## Why this package? | ||
|
||
Laravel and PHP has some lacks, which is fine (they are not designed to be perfect fit for every possible case). Therefore this package serve to fill that gap. | ||
|
||
The package is just a bunch of functions that we most often reuse in plenty of our own open source packages as well as our work. | ||
|
||
## Installation | ||
|
||
Grab this package using composer: | ||
|
||
```bash | ||
composer require open-southeners/laravel-helpers | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Table of contents | ||
|
||
* [Getting started](README.md) | ||
* [Function helpers](function-helpers.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
description: List of functions that works for Laravel Eloquent models. | ||
--- | ||
|
||
# Function helpers | ||
|
||
This is the list of namespaced functions that you can import and use on your Laravel app. | ||
|
||
## model\_from | ||
|
||
Gets model fully qualified class or instanced object from given string. | ||
|
||
```php | ||
\OpenSoutheners\ExtendedPhp\Models\model_from('post'); | ||
// 'App\Models\Post' | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\model_from('post', false); | ||
// object instance for App\Models\Post | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\model_from('post', true, 'Domain\Posts'); | ||
// 'Domain\Posts\Post' | ||
``` | ||
|
||
## is\_model | ||
|
||
Checks if object or class is a Eloquent model. | ||
|
||
```php | ||
\OpenSoutheners\ExtendedPhp\Models\is_model(App\Models\Post::class); | ||
// true | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\is_model(new App\Models\Post()); | ||
// true | ||
``` | ||
|
||
## instance\_from | ||
|
||
Creates a model instance from given key (generally its ID or specified primary key). | ||
|
||
```php | ||
\OpenSoutheners\ExtendedPhp\Models\instance_from(1, App\Models\Post::class); | ||
// object instance for App\Models\Post with ID 1 | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\instance_from(1, App\Models\Post::class, ['id', 'title']); | ||
// object instance for App\Models\Post with ID 1 and only selecting id and title columns | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\instance_from(1, App\Models\Post::class, ['*'], ['author']); | ||
// object instance for App\Models\Post with ID 1 and with author relationship loaded | ||
``` | ||
|
||
## key\_from | ||
|
||
Tries to get Eloquent model key from given variable (can be of multiple types). | ||
|
||
```php | ||
\OpenSoutheners\ExtendedPhp\Models\instance_from('1'); | ||
// 1 | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\instance_from('80b6dc25-8773-4639-abcf-ed1f157deea1'); | ||
// '80b6dc25-8773-4639-abcf-ed1f157deea1' | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\instance_from(App\Models\Post::find(1)); | ||
// 1 | ||
``` | ||
|
||
## query\_from | ||
|
||
Gets always a new Eloquent query builder instance from given model. | ||
|
||
```php | ||
\OpenSoutheners\ExtendedPhp\Models\query_from(App\Models\Post::class); | ||
// object instance for \Illuminate\Database\Eloquent\Builder | ||
|
||
\OpenSoutheners\ExtendedPhp\Models\query_from(App\Models\Post::query()->where('title', 'hello')); | ||
// new object instance for \Illuminate\Database\Eloquent\Builder | ||
``` |