Search
 
SCRIPT & CODE EXAMPLE
 

PHP

autogenerate slug for model laravel

/**
 * Laravel provides a boot method which is 'a convenient place to 
 * register your event bindings.'
 * See: https://laravel.com/docs/master/eloquent#events
 */
public static function boot()
{
    parent::boot();

    // registering a callback to be executed upon the creation of an activity AR
    static::creating(function($activity) {

        // produce a slug based on the activity title
        $slug = Str::slug($news->title);

        // check to see if any other slugs exist that are the same & count them
        $count = static::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();

        // if other slugs exist that are the same, append the count to the slug
        $activity->slug = $count ? "{$slug}-{$count}" : $slug;

    });

}
// Use the create method
Activity::create(['title'=>'lorem ipsum']);

Comment

PREVIOUS NEXT
Code Example
Php :: php do not refresh page after submit post 
Php :: php read csv 
Php :: php pdo postegresql connection 
Php :: laravel execute command from terminal 
Php :: générer des nombres aléatoires décimaux en php 
Php :: laravel validation digits 
Php :: phone number validation, laravel 
Php :: blade if array key exists 
Php :: redirect in php 
Php :: how unique field in table in phpmyadmin 
Php :: if condition in smarty 
Php :: php query pdo 
Php :: apiresource laravel 
Php :: Increase the PHP memory limit 
Php :: remove a specific element from an array php 
Php :: round to 2 decimal places php 
Php :: php secure password hash 
Php :: how to remove annoying plugin notification in wordpress 
Php :: php string left 10 characters 
Php :: http_response_code 
Php :: laravel create new file if not exists 
Php :: php count 
Php :: php unserialize array 
Php :: php object 
Php :: use font awesome in laravel 8 
Php :: change field name in validation laravel 8 
Php :: laravel use variable inside callback function 
Php :: php 8 match 
Php :: wordpress display post categories 
Php :: laravel wheredate 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =