Skip to content

Commit

Permalink
slug is nullable and add validation on column size
Browse files Browse the repository at this point in the history
  • Loading branch information
SethSharp committed Apr 23, 2024
1 parent 92a32b1 commit 449279a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('blogs', function (Blueprint $table) {
$table->string('slug')->nullable()->change();
});
}
};

9 changes: 6 additions & 3 deletions src/Actions/Blogs/UpdateBlogAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class UpdateBlogAction
public function __invoke(Blog $blog, UpdateBlogRequest $updateBlogRequest): Blog
{
// setup slug based on provided input
if ($updateBlogRequest->has('slug')) {
$slug = Str::slug($updateBlogRequest->input('slug'));
} else {
$slug = $updateBlogRequest->input('slug');
if (is_null($slug)) {
$slug = Str::slug($updateBlogRequest->input('title'));
} else {
if (strlen($slug) === 0) {
$slug = Str::slug($updateBlogRequest->input('title'));
}
}

// attach tags
Expand Down
5 changes: 5 additions & 0 deletions src/Requests/Blogs/UpdateBlogRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function rules(): array
'title' => [
'required',
'string',
'max:254',
Rule::unique(Blog::class, 'title')->ignore($this->route('blog')->id),
],
'collection_id' => [
Expand All @@ -37,6 +38,7 @@ public function rules(): array
'nullable',
'string',
'min:10',
'max:254',
Rule::unique(Blog::class, 'slug')->ignore($this->route('blog')->id),
],
'tags' => [
Expand All @@ -47,16 +49,19 @@ public function rules(): array
'nullable',
'required_if:is_draft,false',
'string',
'max:254',
],
'meta_tags' => [
'nullable',
'required_if:is_draft,false',
'string',
'max:254',
],
'meta_description' => [
'nullable',
'required_if:is_draft,false',
'string',
'max:254',
],
'content' => [
'required_if:is_draft,false',
Expand Down

0 comments on commit 449279a

Please sign in to comment.