route_from_model()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
use Illuminate\Database\Eloquent\Model;
4
use TomHart\Routing\RouteBuilder;
5
6
if (!function_exists('route_from_model')) {
7
    /**
8
     * This allows a route to be dynamically built just from a Model instance.
9
     * Imagine a route called "test":
10
     *      '/test/{name}/{id}'
11
     * Calling:
12
     *      route_from_model('test', Site::find(8));
13
     * will successfully build the route, as "name" and "id" are both attributes on the Site model.
14
     *
15
     * Further more, once using "route_from_model", the route can be changed. Without changing the call:
16
     *      route_from_model('test', Site::find(8));
17
     * You can change the route to be:
18
     *      '/test/{name}/{id}/{parent->relationship->value}/{slug}/{otherParent->value}'
19
     * And the route will successfully change, as all the extra parts can be extracted from the Model.
20
     * Relationships can be called and/or chained with "->" (Imagine Model is a Order):
21
     *      {customer->address->postcode}
22
     * Would get the postcode of the customer who owns the order.
23
     *
24
     * @param string  $routeName The route you want to use
25
     * @param Model   $model
26
     * @param mixed[] $data
27
     *
28
     * @return string
29
     */
30
    function route_from_model(string $routeName, Model $model, array $data = [])
31
    {
32
        return app(RouteBuilder::class)->routeFromModel($routeName, $model, $data);
33
    }
34
}
35