Skip to content

Commit

Permalink
add Gitbook docs
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Aug 15, 2024
1 parent 5ee8de9 commit fa81e5b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/README.md
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
```
4 changes: 4 additions & 0 deletions docs/SUMMARY.md
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)
76 changes: 76 additions & 0 deletions docs/function-helpers.md
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
```

0 comments on commit fa81e5b

Please sign in to comment.