title: Laravel generate slug before save
body: <p>Im trying to learn laravel 5 with help of <a href="https://laracasts.com/series/laravel-5-fundamentals/episodes/11?autoplay=true#" rel="noreferrer">this wondefull website</a>.
  For my activity model I want to generate slugs before I save one to my database so I've created the following model.</p>
  <pre><code>&lt;?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-&gt;attributes['slug'] = str_slug($this-&gt;title , &quot;-&quot;);
      }
      //    
  }
  </code></pre>
  <p>But when I save an object with help of the Activity model slug is not filled, i tried changing it to $this-&gt;attributes['title'] = &quot;test&quot; for testing but it didnt run. Also I tried adding parameters $title, $slug to  setSlugAttribute() but it didnt help.</p>
  <p>What am I doing wrong and could someone explain the parameter that is used in some examples for setSomeAttribute($whyParameterHere).</p>
  <p><em>Note : there is a slug field in my database.</em></p>
  <p>As suggested by user3158900 I've tried :</p>
  <pre><code>public function setTitleAttribute($title){
      $this-&gt;title = $title;
      $this-&gt;attributes['slug'] = str_slug($this-&gt;title , &quot;-&quot;);
  }
  //
  </code></pre>
  <p>This makes my title field empty but saves the slug the way I want it, why is $this-&gt;title empty then ?
  If I remove $this-&gt;title = $title; both title and slug are empty</p>