Tags: PHP MySQL
If you want to remove all HTML tags from all fields upon saving a row in Larave, stick this boot method into the model.
/**
* Booting method
*
* @return void
*/
protected static function boot()
{
parent::boot();
// Strip html tags on save
static::creating(function ($model) {
foreach ($model->toArray() as $col => $val) {
// If the column exists in the DB
if (!is_null($model->$col)) {
// Sometimes I have related models eaglerly loaded. Let's exclude those.
if (! is_array($val)) {
$model->$col = strip_tags($val);
}
}
}
});
// Strip html tags on save
static::saving(function ($model) {
foreach ($model->toArray() as $col => $val) {
if (!is_null($model->$col)) {
if (! is_array($val)) {
$model->$col = strip_tags($val);
}
}
}
});
}
©2022, kirillsimin.com