PHPackages                             repat/laravel-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. repat/laravel-helper

ActiveLibrary[Database &amp; ORM](/categories/database)

repat/laravel-helper
====================

Some helper function for developing with Laravel 5+

0.6(2y ago)55.7k1MITPHPPHP &gt;=7.2

Since Dec 9Pushed 2y ago1 watchersCompare

[ Source](https://github.com/repat/laravel-helper)[ Packagist](https://packagist.org/packages/repat/laravel-helper)[ Docs](https://repat.de)[ RSS](/packages/repat-laravel-helper/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (2)Versions (58)Used By (0)

laravel-helper
==============

[](#laravel-helper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/85dac8ab17e40748ab872e474f5bcd260f8f7fe34dae47ec1f9dcb1b2b051838/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72657061742f6c61726176656c2d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/repat/laravel-helper)[![Total Downloads](https://camo.githubusercontent.com/bd5d3fc652176774d1c667086ed332e66a70cf3292146e05129d30706ab650c7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72657061742f6c61726176656c2d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/repat/laravel-helper)

**laravel-helper** is a package full of helper functions I found useful when developing applications with Laravel. All functions are wrapped with a `functions_exists()` in case of conflicts.

Also have a look at

-
-  (abandoned?)
-
-

Ideas what should go in here? Write a pull request or email!

Installation
------------

[](#installation)

`$ composer require repat/laravel-helper`

Documentation
-------------

[](#documentation)

> ⚠️ The majority of helper functions are now in repat/php-helper which this package is based on. You can find the documentation at

### Database

[](#database)

#### `mysql_headers($table, $assoc = false)`

[](#mysql_headerstable-assoc--false)

Returns an array of MySQL headers/columns or empty array in case of an error. If the second parameter is set `true` (default is `false`) it returns an associative array.

```
print_r(mysql_headers("test_table"));
// returns: Array( [0] => head1, [1] => head2 )

print_r(mysql_headers("test_table", $assoc = true));
// returns: Array( [head1] => head1, [head2] => head2)
```

#### `table_headers($model)`

[](#table_headersmodel)

Returns the database table headers, similar to `mysql_headers()`, but based on an object of a Eloquent Model.

```
use App\Models\User;

$user = User::first();

print_r(table_headers($user));
// returns: Array( 1 => id, 2 => name, ...)
```

#### `print_db_session($table = 'sessions')`

[](#print_db_sessiontable--sessions)

`print_r()` the session of current user.

```
print_db_session();
// returns:
// Array
// (
//     [_token] => 7Au0aYkJVxQVA3xQBfdJwKNaWxUv0UVJKublCqMn
//     [locale] => en
//     ...
// )
```

#### `get_free_slug($toSlug, $field, $fqcn, $id, $pk)`

[](#get_free_slugtoslug-field-fqcn-id-pk)

Returns a unique slug for an Eloquent Model given the following parameters:

- `$toSlug`: suggestion for the slug
- `$field`: name of the database field, usually `slug`
- `$fqcn`: Fully qualified class name of Eloquent Model
- `$id`: id to exclude (e.g. it's own on update)
- `$pk`: primary key of the database table, defaults to `id`

Will append a number if `$toSlug` is already taken.

```
use App\Model\User;

$user = User::first();

$user->id;
// returns: 1
$user->slug;
// returns: foobar

get_free_slug('foobar', 'slug', User::class, 1, 'id');
// returns: foobar1
```

#### `insert_bindings($query)`

[](#insert_bindingsquery)

Inserts values into `?` from the `->toSql()` string.

```
insert_bindings(DB::table('users')->where('id', 1));
// returns: SELECT * FROM `users` WHERE `id` = '1'
```

### Object

[](#object)

#### `morph_map()`

[](#morph_map)

Returns the morphMap from `AppServiceProvider` set with `Relation::morphMap()`.

```
morph_map();
// returns:Array
// (
//     [user] => App\Models\User
// )
```

#### `morph_map_key($fqcn)`

[](#morph_map_keyfqcn)

Reverse lookup for a class in the morphMap of the `AppServiceProvider` set with `Relation::morphMap()`.

```
use App\Models\User;

morph_map_key(User::class);
// returns: 'user'
```

#### `cache_get_or_add($key, $callable)`

[](#cache_get_or_addkey-callable)

Returns Cache for given key or adds the return value from the callable to the cache and then returns it.

```
use App\Models\Post;

$posts = cache_get_or_add('posts', function() {
    return Post::orderBy('created_at', 'desc')->get();
});
```

#### `dispatch_tinker($job)`

[](#dispatch_tinkerjob)

Dispatches jobs from the [tinker REPL](https://laravel.com/docs/6.x/artisan#tinker).

```
dispatch_tinker(new \App\Jobs\CleanupJob());
// returns: 1 (id of job)
```

### Networking

[](#networking)

#### `route_path($path)`

[](#route_pathpath)

Get the path to the Laravel routes folder, similar to `app_path()`, see [Helpers Documentation](https://laravel.com/docs/5.8/helpers). It will append `$path` but it's not mandatory.

```
route_path();
// returns: /var/www/htdocs/laravel/routes

route_path('web.php');
// returns: /var/www/htdocs/laravel/routes/web.php
```

#### `named_routes($path, $verb)`

[](#named_routespath-verb)

Returns array of all named routes in a routes file or `null` on error. It's possible to pass an HTTP verb/method defined in `HTTP_VERBS_LARAVEL` (see below).

```
named_routes('/var/www/htdocs/laravel/routes/web.php');
// returns: [
// 'laravel.get'
// 'laravel.post'
// ]

named_routes('/var/www/htdocs/laravel/routes/web.php', 'get');
// returns: [
// 'laravel.get'
// ]
```

##### `current_route_name()`

[](#current_route_name)

If the current route has a name, otherwise return `null`.

```
// in routes/web.php
// Route::name('dev.foo')->get('foo', 'Dev\TestController@foo');
// Route::get('bar', 'Dev\TestController@bar');

// in Dev/TestController@foo
current_route_name();
// returns: dev.foo

// in Dev/TestController@foo
current_route_name();
// returns: null
```

##### `all_routes()`

[](#all_routes)

Returns an array of all routes like so:

```
all_routes();
// returns:
//      "name" => 'route.test', // could be null
//      "methods" => [
//          "GET",
//          "HEAD",
//      ],
//      "uri" => "test",
//      "action" => "\App\Http\Controllers\TestController@test",
```

##### `route_exists($namedRoute)`

[](#route_existsnamedroute)

Checks if the given route is a named route in any routes file.

```
route_exists('route.test');

// returns: true

route_exists('route.foobar')

// returns: false
```

### Optional Packages

[](#optional-packages)

Optional packages suggested by this are required for these functions to work.

#### `translated_attributes($fqcn)`

[](#translated_attributesfqcn)

Uses [astrotomic/laravel-translatable](https://github.com/astrotomic/laravel-translatable) and Reflection to get the `translatedAttributes` attribute of a Model.

- `$ composer require astrotomic/laravel-translatable`

```
use App\Models\Product;

translated_attributes(Product::class);
// returns: ['title', 'description'];
```

### HTML

[](#html)

#### `extract_inline_img($text, $storagePath, $srcPath, $optimize)`

[](#extract_inline_imgtext-storagepath-srcpath-optimize)

Extracts an inline image from a text, saves it on the harddrive and puts in the filename with the [src](https://developer.mozilla.org/de/docs/Web/HTML/Element/img) attribute. Can use the [spatie/laravel-image-optimizer](https://github.com/spatie/laravel-image-optimizer) to optimize images after upload but it's disabled by default.

- `$ composer require spatie/laravel-image-optimizer`

```
extract_inline_img("
