Giter VIP home page Giter VIP logo

arabify's Introduction

Generate slugs when saving Laravel Eloquent models

| This Is Just A Wrapper Package To Support Arabic Languages Slug

Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.

$model = new Article();
$model->name = 'كيف اتعلم البرمجة';
$model->save();

echo $model->slug; // ouputs "كيف-اتعلم-البرمجة"

Installation

Update your project's composer.json file.

composer require zymawy/arabify

You may want to change the function you can do change it from the config

php artisan vendor:publish --provider="Zymawy\Arabify\ArabifyServiceProvider"

Usage

Your Eloquent models can use the Zymawy\Arabfiy\HasSlug trait and the Zymawy\Arabfiy\SlugOptions class.

The trait has a protected method getSlugOptions() that you can implement for customization.

Here's an example:

class YourEloquentModel extends Model
{
    use HasSlug;
    
    /**
     * This function is optional and only required
     * when you want to override the default behaviour
     */
    protected function getSlugOptions()
    {
        return SlugOptions::create()
            ->slugSeperator('-')
            ->generateSlugFrom('name')
            ->saveSlugTo('slug');
    }
}

If you want to generate your slug from a relationship.

class YourEloquentModel extends Model
{
    use HasSlug;
    
    public function getNameAndFooAttribute()
    {
        $name = $this->name;
        if ($this->foo) {
            $name .= " {$this->foo->name}";
        }

        return $name;
    }
    
    protected function getSlugOptions()
    {
        return SlugOptions::create()
            ->generateSlugFrom('name_and_foo');
    }
}

Config

You do not have to add the method in you model (the above will be used as default). It is only needed when you want to change the default behaviour.

By default it will generate a slug from the name and save to the slug column.

It will suffix a -1 to make the slug unique. You can disable it by calling makeSlugUnique(false).

It will use the - as a separator. You can change this by calling slugSeperator('_').

You can use multiple fields as the source of the slug generateSlugFrom(['firstname', 'lastname']).

You can also pass a callable function to generateSlugFrom().

Have a look here for the options and available config functions.

Credits

See also the list of contributors who participated in this project.

arabify's People

Contributors

dependabot-preview[bot] avatar zymawy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.