PHPackages                             cogniteq/maxfactor-support - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. cogniteq/maxfactor-support

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cogniteq/maxfactor-support
==========================

Support and helpers for Laravel PHP based projects

2.7.7(5y ago)04MITPHP

Since Jan 31Pushed 5y agoCompare

[ Source](https://github.com/Cogniteq/maxfactor-laravel-support)[ Packagist](https://packagist.org/packages/cogniteq/maxfactor-support)[ RSS](/packages/cogniteq-maxfactor-support/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (7)Versions (52)Used By (0)

Maxfactor Laravel Support
=========================

[](#maxfactor-laravel-support)

[![Packagist](https://camo.githubusercontent.com/552cedf52ddb143a73ab1cd6814bba33a09aded8be3738a7e300c9c566689426/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6178666163746f722f737570706f72742e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/maxfactor/support)

Authors:

- [Marco Mark](mailto:marco.mark@dewsign.co.uk)
- [Jacob Walters](mailto:jacob.walters@dewsign.co.uk)
- [Tristan Ward](mailto:tristan.ward@dewsign.co.uk)
- [Sam Wrigley](mailto:sam.wrigley@dewsign.co.uk)
- [Daniel Crewdson](mailto:daniel.crewdson@dewsign.co.uk)

Overview
--------

[](#overview)

A set of helper methods and classes commonly used in Web projects

Models
------

[](#models)

More generic helpers which apply to more than just front-end facing web pages.

### Active State

[](#active-state)

Ensure records can be active or inactive (e.g. Draft like state). Add the trait to your model and the fields to your migration.

```
use Maxfactor\Support\Model\Traits\HasActiveState;
```

```
$table->active();
```

#### Prevent access to inactive models

[](#prevent-access-to-inactive-models)

By default, the config option `canViewInactive` is set to `true`, so all requests to view content will be authorised. To prevent unauthorised access to content that isn't active, either register a global `viewInactive` gate in the service provider, or on a per-model basis via a Policy. If the Gate fails, the package will abort a `503` error.

```
public function viewInactive($user, $model)
{
    if (config('maxfactor-support.canViewInactive')) {
        return true;
    }

    // Your logic... (Gate::allows() etc)

    return false;
}
```

### Featured State

[](#featured-state)

Allow records to be featured.

```
use Maxfactor\Support\Model\Traits\CanBeFeatured;
```

```
$table->featured();
```

### Sorting Order

[](#sorting-order)

Allow records to be ordered/sorted (sequentially). Add the trait and sortable contract to your model.

```
use Spatie\EloquentSortable\Sortable;
use Maxfactor\Support\Model\Traits\HasSortOrder;

class Category extends Model implements Sortable
{
    use HasSortOrder;
    ...
}
```

Migration helper ...

```
$table->sortable();
```

A query scope is included so you can access sorted results without manually defining the `orderBy`.

```
$results = Model::sorted()->get();
```

Webpage
-------

[](#webpage)

A set of helpers and traits relating to public facing web pages.

Typically any model which interfaces with the front-end views should extend the `Maxfactor\Support\Webpage\Model` instead of the default Eloquent Model. This will ensure other Traits can be correctly initialised.

INPORTANT: In order to try and create some consistancy, the main Model passed into a view should always be called `$page`. This makes it much easier to include default behaviour inside layouts and other shared components without explicitly passing in values.

```
return view('my.view')->with('page', $model)
```

### Meta (incl. Browser Title)

[](#meta-incl-browser-title)

Add the `Maxfactor\Support\Webpage\Traits\HasMetaAttributes` trait to your model and include the migrations by adding the meta field(s). This will include the correct database column and default to cover:

- h1
- browserTitle
- metaDescription
- navTitle

```
$table->meta();
```

Include this in your layout to render the browser title

```
@render('maxfactor:webpage::browserTitle', ['title' => Arr::get($page ?? [], 'browserTitle', config('app.name'))])
```

And this is a pre-made component to render the meta description

```
@render('maxfactor:webpage::metaDescription', ['description' => Arr::get($page ?? [], 'metaDescription')])
```

#### Additional Meta Fields in Nova

[](#additional-meta-fields-in-nova)

You can pass in additional meta fields into the `MetaAttributes::make()` function, to display your own fields in the 'Meta Attributes' panel that this package creates.

```
// Define our additional fields in the resource.
protected function additionalMetaFields()
{
    return [
        Text::make('Example Meta Content'),
    ];
}

// Inside the resources fields() function we can pass in our additional fields as a parameter.
MetaAttributes::make($this->additionalMetaFields()),
```

### Slugs

[](#slugs)

Add the `Maxfactor\Support\Webpage\Traits\HasSlug` trait to your model if you want the route helper to use the `slug` column in your database to identify records. (E.g. `/blog/article/foo` instead of `/blog/article/1`).

Within your migrations you can simply use the slug helper to add the correct column to your tables. `$table->slug()`.

### Breadcrumbs

[](#breadcrumbs)

Use the `Maxfactor\Support\Webpage\Traits\HasBreadcrumbs` trait (already included if you are using the Webpage Model). Overload the `seeds()` method to return custom breadcrumbs and/or use the `seed()` method to push any breadcrumbs you require into the parent seed to add additional breadcrumbs.

```
public function seeds()
{
    return array_merge(parent::seeds(), [[
        'name' => __('Branch finder'),
        'url' => route('branch.index'),
    ], [
        'name' => $this->navTitle,
        'url' => route('branch.show', $this),
    ]]);
}
```

```
// Branch Controller
$branch->seed($name = __('Audiologists'), $url = route('branch.audiologists', $branch), $status = null);
```

Inside your blade view, render the breadcrumbs `@include('maxfactor::components.breadcrumb', ['seed' => $page->breadcrumbs])`. Replace the view with your own if required.

#### Setting a default 'Home' breadcrumb

[](#setting-a-default-home-breadcrumb)

To change the default Home breadcrumb, you can set the `homeBreadcrumb` config value.

```
// config/maxfactor-support.php
'homeBreadcrumb' => 'Home',
```

### Canonicals

[](#canonicals)

Include the `MustHaveCanonical` trait which by default will return the current url but you typically would specify your own.

This can be done on the Model ...

```
public function baseCanonical()
{
    return route('branch.show', $this);
}
```

Or inline (e.g. in the controller). This will take precedence over the `baseCanonical`.

```
$branch->canonical($url);
```

Inside the head of your blade layout (or view) render the canonical meta field.

```
@include('maxfactor::components.canonical')
```

The Maxfactor facade also includes an additional helper method which can be used to deal with Querystring parameters in the canonical url. The `urlWithQuerystring` method accepts the desired url and a string or regular expression to filter which parameters to include in the output. The query values are taken from the current request query, even though the destination url doesn't have to match this. Here's an example of how this could be used to include pagination.

```
// Assuming the current url is https://example.com/journal?page=2&tag=news

Maxfactor::urlWithQuerystring('https://example.com/blog', $allowedParameters = 'page=[^1]');

// returns https://example.com/blog?page=2
```

### Sitemap

[](#sitemap)

To generate a sitemap simply create a new Command and Extend the `MakeSitemap` command in this package. Then add it to your console kernel and schedule as required. Here is the template to use as starting point.

```
