moshemo
1 week ago
143
2
Laravel

Automatically Create Slug from Title Using Create & Validate Methods

Posted 1 week ago by moshemo

I want to automatically create a slug and save it to the database based on the title entered into a form. Currently, this is how my controller is set up:

  public function store(News $id) {
    News::create($this->validateArticle());
    return redirect('/news');
  }

  public function validateArticle() {
    return request()->validate([
      'title' => 'required',
      'excerpt' => 'nullable',
      'body' => 'nullable'
    ]);
  }

How can I modify this code so that I automatically generate a slug based off of the title?

Thanks.

Please sign in or create an account to participate in this conversation.