diff --git a/app/Http/Controllers/PostsController.php b/app/Http/Controllers/PostsController.php index cc7669d..723ecbf 100644 --- a/app/Http/Controllers/PostsController.php +++ b/app/Http/Controllers/PostsController.php @@ -6,10 +6,10 @@ use App\Notifications\PostDeleted; use App\Notifications\PostEdited; use App\Post; -use App\Post_tag; +use App\PostTag; use App\Tag; -use App\User; use Illuminate\Http\Request; +use Illuminate\Validation\Rule; class PostsController extends Controller { @@ -19,6 +19,16 @@ public function __construct() $this->middleware('can:update,post')->except(['index', 'userPosts', 'adminIndex', 'create', 'store']); } + public function validateRequest($request, $post) + { + return $request->validate([ + 'code' => ['required', 'regex:/[a-zA-Z0-9_-]+/', Rule::unique('posts')->ignore($post->id)], + 'name' => 'required|min:5|max:100', + 'description' => 'required|max:255', + 'text' => 'required' + ]); + } + public function index() { $posts = Post::with('tags')->latest()->get(); @@ -43,12 +53,7 @@ public function create() public function store(Request $request) { - $attr = $request->validate([ - 'code' => 'required|unique:posts|regex:/[a-zA-Z0-9_-]+/', - 'name' => 'required|min:5|max:100', - 'description' => 'required|max:255', - 'text' => 'required' - ]); + $attr = $this->validateRequest($request, new Post()); if ($request->has('published')) { $attr['published'] = 1; @@ -58,6 +63,14 @@ public function store(Request $request) $post = Post::create($attr); + if (!is_null($request['tags'])) { + $requestTags = explode(', ', $request['tags']); + foreach ($requestTags as $tag) { + $tag = Tag::firstOrCreate(['name' => $tag]); + $post->tags()->attach($tag); + } + } + sendMailNotifyToAdmin(new PostCreated($post)); flash( 'Post created successfully'); @@ -76,11 +89,7 @@ public function edit(Post $post) public function update(Request $request, Post $post) { - $values = $request->validate([ - 'name' => 'required|min:5|max:100', - 'description' => 'required|max:255', - 'text' => 'required' - ]); + $values = $this->validateRequest($request, $post); $post->update($values); @@ -105,7 +114,7 @@ public function update(Request $request, Post $post) if ($deleteTags->isNotEmpty()) { foreach ($deleteTags as $tag) { $post->tags()->detach($tag); - $isLastTag = Post_tag::where('tag_id', $tag->id)->first(); + $isLastTag = PostTag::where('tag_id', $tag->id)->first(); if (!$isLastTag) $tag->delete(); }; } diff --git a/app/PostTag.php b/app/PostTag.php new file mode 100644 index 0000000..840658a --- /dev/null +++ b/app/PostTag.php @@ -0,0 +1,10 @@ + 'datetime', ]; - public function posts() { + public function posts() + { return $this->hasMany(Post::class, 'owner_id'); } } diff --git a/app/View/Components/PostForm.php b/app/View/Components/PostForm.php new file mode 100644 index 0000000..057609b --- /dev/null +++ b/app/View/Components/PostForm.php @@ -0,0 +1,20 @@ +post = $post; + } + + public function render() + { + return view('components.post-form', ['post' => $this->post]); + } +} diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index d6b82cd..2044b07 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -21,8 +21,7 @@ public function up() $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); - $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); - $table->timestamp('updated_at'); + $table->timestamps(); }); } diff --git a/database/migrations/2020_08_26_090725_create_posts_table.php b/database/migrations/2020_08_26_090725_create_posts_table.php index 2abe5e0..1b4ad94 100644 --- a/database/migrations/2020_08_26_090725_create_posts_table.php +++ b/database/migrations/2020_08_26_090725_create_posts_table.php @@ -20,9 +20,8 @@ public function up() $table->string('name', 100); $table->string('description', 255); $table->text('text'); - $table->boolean('published')->default(0); - $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); - $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP')); + $table->boolean('published')->default(false); + $table->timestamps(); $table->foreignId('owner_id')->constrained('users')->onDelete('cascade'); }); diff --git a/database/migrations/2020_08_26_103233_create_contacts_table.php b/database/migrations/2020_08_26_103233_create_contacts_table.php index 5af7372..1e7026a 100644 --- a/database/migrations/2020_08_26_103233_create_contacts_table.php +++ b/database/migrations/2020_08_26_103233_create_contacts_table.php @@ -18,8 +18,7 @@ public function up() $table->id(); $table->string('email'); $table->text('message'); - $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); - $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP')); + $table->timestamps(); }); } diff --git a/database/migrations/2020_08_28_124336_create_feedback_table.php b/database/migrations/2020_08_28_124336_create_feedback_table.php index a6cd127..32a315d 100644 --- a/database/migrations/2020_08_28_124336_create_feedback_table.php +++ b/database/migrations/2020_08_28_124336_create_feedback_table.php @@ -18,8 +18,7 @@ public function up() $table->id(); $table->string('email'); $table->text('text'); - $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); - $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP')); + $table->timestamps(); }); } diff --git a/database/migrations/2020_09_07_173528_create_tags_table.php b/database/migrations/2020_09_07_173528_create_tags_table.php index f27c55c..a574b7f 100644 --- a/database/migrations/2020_09_07_173528_create_tags_table.php +++ b/database/migrations/2020_09_07_173528_create_tags_table.php @@ -17,8 +17,7 @@ public function up() Schema::create('tags', function (Blueprint $table) { $table->id(); $table->string('name')->unique(); - $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); - $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP')); + $table->timestamps(); }); } diff --git a/resources/views/components/post-form.blade.php b/resources/views/components/post-form.blade.php new file mode 100644 index 0000000..88592c0 --- /dev/null +++ b/resources/views/components/post-form.blade.php @@ -0,0 +1,108 @@ +
+
+ + +
+ Code is required. +
+ + @error('code') +
+ {{ $message }} +
+ @enderror +
+ +
+ + + +
+ Post Name is required. +
+ + @error('name') +
+ {{ $message }} +
+ @enderror +
+ +
+ + + +
+ Post Description is required. +
+ + @error('description') +
+ {{ $message }} +
+ @enderror +
+ +
+ + + +
+ Post Description is required. +
+ + @error('text') +
+ {{ $message }} +
+ @enderror +
+ +
+ + +
+ +
+ published)) checked @endif + > + +
+
diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index cb7b163..5e72341 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -15,24 +15,24 @@ @foreach($posts as $post)
-
+
Post #{{ $post->id }}

{{ $post->name }}

{{ $post->created_at->toFormattedDateString() }}
-

{{ str_limit($post->text, $limit = 100, $end = '...') }}

+

{{ str_limit($post->text, $limit = 100, $end = '...') }}

@if($post->tags->isNotEmpty()) @endif - Continue reading + Continue reading
diff --git a/resources/views/layouts/base/header.blade.php b/resources/views/layouts/base/header.blade.php index e539224..5fe9989 100644 --- a/resources/views/layouts/base/header.blade.php +++ b/resources/views/layouts/base/header.blade.php @@ -12,7 +12,7 @@