Search
 
SCRIPT & CODE EXAMPLE
 

PHP

action after model is created 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 :: fresh migrqte laravel 
Php :: laravel delete relationship data 
Php :: if browser url is having domain name in it check using php 
Php :: laravel drop foreign key 
Php :: laravel validate datetime with datetime-local 
Php :: getting input value in session variable in php 
Php :: how increase php upload size in wordpress 
Php :: how to run multiple seeder at a time in laravel 
Php :: laravel grouping where 
Php :: Laravel assets url issue 
Php :: how to get correct file or content mime type using/in php 
Php :: php sum of digits 
Php :: number format without comma php 
Php :: pdo prepare 
Php :: php ternary shorthand 
Php :: use resource in laravel 8 
Php :: php get object class 
Php :: check if not empty blade engine 
Php :: laravel wire not working 
Php :: php set http status header 
Php :: laravel eloquent group by week 
Php :: laravel create mode 
Php :: appserviceprovider laravel auth user 
Php :: guzzle Request with POST files 
Php :: cronjob php linux 
Php :: php add to multidimensional array 
Php :: laravel controller create command in a folder 
Php :: laravel local file storage 
Php :: php loops 
Php :: php foreac 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =