fomvasss/laravel-eus

A package for generate eloquent unique string value

Installs: 125

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:composer-package

0.5.0 2020-03-02 21:11 UTC

This package is auto-updated.

Last update: 2024-10-29 05:10:57 UTC


README

License Build Status Latest Stable Version Total Downloads Quality Score

With this package you can generate unique string value for eloquent entity model.

Installation

Run from the command line:

composer require fomvasss/laravel-eus

Publish the configurations

Run from the command line:

php artisan vendor:publish --provider="Fomvasss\LaravelEUS\ServiceProvider"

A configuration file will be publish to config/eus.php. In config file you can set default options.

Usage

Use EUS facade in your Laravel project

In next example, we generate unique article slug:

<?php 

namespace App\Http\Controllers;

use Fomvasss\LaravelEUS\Facades\EUS;
use Illuminate\Http\Request;

class ArticleController extends Controller 
{
    
    public function store(Request $request)
    {
        $rawSlug = $request->get('slug', $request->name);
        $slug = EUS::setEntity(new \App\Models\Article())
            ->setRawStr($rawSlug)
            ->get();
                
        $article = \App\Model\Article::create([
            'title' => $title,
            'slug' => $slug,
            //.. other data
        ]);
        
        //..
    }

    public function update($id, Request $request) 
    {        
        $article = \App\Model\Article::fingOrFail($id);
     
        $rawSlug = $request->get('slug', $request->name);
        $slug = EUS::setEntity($article)
            ->setRawStr($rawSlug)
            ->get();
         
        $article->update([
             'title' => $title,
             'slug' => $slug,
             //.. other data
         ]);
        //..
    }
}

Other alloved methods

Generate and get unique URL path:

<?php
$urlUniqueAlias = EUS::setEntity(new \App\Models\UrlAlias())
    ->setRawStr('path/for-your/unique-page')
    ->setFieldName('alias')
    ->setSlugSeparator('-')
    ->setAllowedSeparator('/')
    ->get();

Generate and save unique system name:

<?php
$term = \App\Models\Term::find(1);
EUS::setEntity($term)
    ->setRawStr($term->name)
    ->setFieldName('system_name')
    ->setSlugSeparator('_')
    ->where(['locale', '=', 'de']) // additional condition for finding a unique value
    ->save();

Use Dependency Injection

<?php

namespace App\Some\Namespace;

use Fomvasss\LaravelEUS\EUSGenerator;

class MyClass
{
    public function __construct(EUSGenerator $eus)
    {
        $this->eus = $eus;
    }
    
    public function example()
    {
        $article = \App\Models\Article::find(1);
        $this->eus
            ->setEntity($article)
            ->setRawStr('Some string for slug')
            ->save();
    }
}