Laravel generates mucus before storage

I am trying to learn laravel 5 using this wondefull site . For my activity model, I want to generate bullets before storing it in my database, so I created the following model.

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Activity extends Model { protected $table = 'activitys'; protected $fillable = [ 'title', 'text', 'subtitle' ]; // Here I want to auto generate slug based on the title public function setSlugAttribute(){ $this->attributes['slug'] = str_slug($this->title , "-"); } // } 

But when I save the object using the Activity model, the slug does not fill up, I tried changing it to $ this-> attributes ['title'] = "test" for testing, but it did not start. I also tried adding the parameters $ title, $ slug to setSlugAttribute (), but it did not help.

What am I doing wrong and can someone explain the parameter used in some examples for setSomeAttribute ($ whyParameterHere).

Note: there is an empty field in my database.

As suggested by user 3158900, I tried:

 public function setTitleAttribute($title){ $this->title = $title; $this->attributes['slug'] = str_slug($this->title , "-"); } // 

This makes my header field empty, but keeps the pool the way I want it, why is this $ this-> title empty then? If I remove $ this-> title = $ title; both the header and the pool are empty

+7
php laravel laravel-5
source share
4 answers

I believe this does not work because you are not trying to set the slug attribute so that the function never comes across.

I suggest setting $this->attributes['slug'] = ... in your setTitleAttribute() function so that it setTitleAttribute() whenever you set the header.

Otherwise, another solution would be to create a save event for your model that would set it there.

Edit: according to the comments, it is also necessary to set the title attribute in this function ...

 public function setTitleAttribute($value) { $this->attributes['title'] = $value; $this->attributes['slug'] = str_slug($value); } 
+13
source share

One way to achieve this would be to connect to sample models . In this case, we want to create slug at creation .

 /** * Laravel provides a boot method which is 'a convenient place to register your event bindings.' * See: https://laravel.com/docs/4.2/eloquent#model-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; }); } 

You also need to add the following to the list of alias applications (app.php):

 'Str' => Illuminate\Support\Str::class, 
+4
source share

You can use this package, which I use https://github.com/cviebrock/eloquent-sluggable or check how it applies the observer to save the model and how it generates a unique Slug, then do the same.

+2
source share

You want to set a header based slug when the title attribute is set.

 public function setTitleAttribute($value) { $this->attributes['title'] = $value; $this->attributes['slug'] = str_slug($value); } /// Later that same day... $activity->title = 'Foo Bar Baz'; echo $activity->slug; // prints foo-bar-baz 

Another alternative would be to use ModelObserver and listen for events while saving . This will allow you to generate a pool right before writing the model to the database.

 class ActivityObserver { public function saving($activity) { $activity->slug = str_slug($activity->title); } } 

In both cases, you probably want to add some logic to check if slug exists in the database by adding a number increment if that happens. those. foo-bar-baz-2 . The safest place for this logic will be in ModelObserver, as it runs just before the write action.

+1
source share

All Articles