GithubHelp home page GithubHelp logo

Comments (3)

chard1988 avatar chard1988 commented on June 10, 2024

What I've tested so far is listening for creating and updating events on the course model with the code as follows

`
protected static function boot()
{
parent::boot();

/**
 * Listen for the creating event.
 * Clean course_name and set as slug
 */
 static::creating(function ($model) {
     $name = strip_tags($model->course_name);
     $model->setAttribute('slug', Str::slug($name));
 });

 /**
  * Listen for the updating event.
  * Clean course_name and set as slug
  */
  static::updating(function ($model) {
      $name = strip_tags($model->course_name);
      $model->setAttribute('slug', Str::slug($name));
  });

}
`

Is there a better or cleaner way to achieve the same result?

from eloquent-sluggable.

chard1988 avatar chard1988 commented on June 10, 2024

Retracted my code and just used the method.

image

from eloquent-sluggable.

cviebrock avatar cviebrock commented on June 10, 2024

Hi @chard1988 .. I was going to suggest something similar: create a model method that does the HTML-stripping for you, and then use that as the source. e.g.:

public function sluggable(): array {
  return [
    'slug' => [
      'source' => 'stripped_source'
    ]
  ];
}

public function getStrippedSourceAttribute(): string {
  return strip_tags($this->courseName . ' ' . $this->UUID);
}

Your solution works just as well. Either of those would probably be preferable to linking into the event system.

from eloquent-sluggable.

Related Issues (20)

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.