logo
We provide creative solutions in form of web and mobile applications to clients around the world, creating apps that get attention and meaningful.

Web Development

Mobile Applications Development

Business Solutions

Odoo ERP Implementation

Wordpress Plugins

WordPress Themes

Laravel Development

Server Security

Server Administration

Mobile Marketing

Pay Per Click (PPC) Management

Conversion Rate Optimization

Email Marketing

Online Presence Analysis

Fell Free To contact Us
We are available from Monday to Friday 9AM to 6PM.

+92-323-5531561

talk@appknitters.com

Lahore, Punjab, Pakistan

+92-323-5531561

Lahore, Punjab, Pakistan

Top

Laravel Eloquent: Model boot() method

Model’s boot() method is a mysterious spot in Laravel’s Eloquent model where you can overdrive default behavior or default field values:


class User extends Model
{
    public static function boot()
    {
        parent::boot();
        static::updating(function($model)
        {
            // do some logging
            // override some property like $model->something = transform($something);
        });
    }
}

An example of usage of boot() method can be setting the value of a field at the time of creating the model object, e.g. generating slug of page or post currently being edited.


public static function boot()
{
  parent::boot();
  self::creating(function ($model) {
    $model->slug = str_slug($title, $separator);
  });
}

Having more than six years of experince developing custom applications with Laravel, WordPress, Zend, and Cordova.

No Comments

Post a Comment