Skip to content

Commit

Permalink
Comment on Post
Browse files Browse the repository at this point in the history
  • Loading branch information
wasilolly committed Oct 8, 2021
1 parent 6a774d0 commit 686d5d3
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 20 deletions.
25 changes: 25 additions & 0 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class CommentController extends Controller
{

public function store(Post $post)
{
request()->validate([
'body' => 'required'
]);


$post->comments()->create([
'body' => request()->body,
'user_id' => auth()->id()
]);

return back();
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/SessionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function store()
return redirect('/')->with('success', 'Welcome Back!');
}
return back()->withInput()
->withErrors(['email' => 'credentials could not be verified']);
->withErrors(['email' => 'credentials could not be verified']);
}
public function destroy()
{
Expand Down
20 changes: 20 additions & 0 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
use HasFactory;

public function post()
{
return $this->belongsTo(Post::class);
}
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
}
3 changes: 3 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Post extends Model

protected $with = ['category','author'];

public function comments(){
return $this->hasMany(Comment::class);
}
public function category(){
return $this->belongsTo(Category::class);
}
Expand Down
3 changes: 2 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;

Expand All @@ -24,6 +25,6 @@ public function register()
*/
public function boot()
{
//
Model::unguard();
}
}
32 changes: 32 additions & 0 deletions database/factories/CommentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Database\Factories;

use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class CommentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Comment::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'post_id' => Post::factory(),
'user_id' => User::factory(),
'body' => $this->faker->paragraph(2)
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('category_id');
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id');
$table->string('title');
$table->string('slug');
Expand Down
35 changes: 35 additions & 0 deletions database/migrations/2021_10_08_121457_create_comments_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->text('body');
$table->timestamps();

});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
17 changes: 17 additions & 0 deletions resources/views/components/post-comment.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@props(['comment'])
<section class="col-span-8 col-start-5 mt-2">
<article class="flex bg-gray-100 border border-gray-200 p-6 rounded-xl space-x-4">
<div class="flex-shrink-0">
<img src="https://i.pravatar.cc/60?u=id{{$comment->user_id}}" width="60" height="60" alt="">
</div>
<div>
<header>
<h3 class="font-bold">{{$comment->author->username}}</h3>
<p class="text-xs">Posted
<time>{{ $comment->created_at->format('F, j, Y, g:i a')}}</time>
</p>
<p class="mt-3 text-s">{{$comment->body}}</p>
</header>
</div>
</article>
</section>
54 changes: 44 additions & 10 deletions resources/views/posts/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@
<img src="/images/illustration-1.png" alt="" class="rounded-xl">

<p class="mt-4 block text-gray-400 text-xs">
Published <time>{{$post->created_at->diffForHumans() }}</time>
Published <time>{{ $post->created_at->diffForHumans() }}</time>
</p>

<div class="flex items-center lg:justify-center text-sm mt-4">
<img src="/images/lary-avatar.svg" alt="Lary avatar">
<div class="ml-3 text-left">
<h5 class="font-bold">
<a href="/?author/{{$post->author->username}}">{{$post->author->name}}</a>
<a href="/?author/{{ $post->author->username }}">{{ $post->author->name }}</a>
</h5>
</div>
</div>
</div>

<div class="col-span-8">
<div class="hidden lg:flex justify-between mb-6">
<a href="/" class="transition-colors duration-300 relative inline-flex items-center text-lg hover:text-blue-500">
<a href="/"
class="transition-colors duration-300 relative inline-flex items-center text-lg hover:text-blue-500">
<svg width="22" height="22" viewBox="0 0 22 22" class="mr-2">
<g fill="none" fill-rule="evenodd">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
<path stroke="#000" stroke-opacity=".012" stroke-width=".5"
d="M21 1v20.16H.84V1z">
</path>
<path class="fill-current" d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z">
<path class="fill-current"
d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z">
</path>
</g>
</svg>
Expand All @@ -37,21 +40,52 @@
</a>

<div class="space-x-2">
<a href="/categories/{{$post->category->slug}}" class="px-3 py-1 border border-blue-300 rounded-full text-blue-300 text-xs uppercase font-semibold" style="font-size: 10px">{{$post->category->name}}</a>
<a href="/categories/{{ $post->category->slug }}"
class="px-3 py-1 border border-blue-300 rounded-full text-blue-300 text-xs uppercase font-semibold"
style="font-size: 10px">{{ $post->category->name }}</a>

</div>
</div>

<h1 class="font-bold text-3xl lg:text-4xl mb-10">
{{$post->title}}
{{ $post->title }}
</h1>

<div class="space-y-4 lg:text-lg leading-loose">
<p>{{$post->body}}</p>
<p>{{ $post->body }}</p>
</div>
</div>
{{-- Comment Section --}}
<section class="col-span-8 col-start-5 mt-5">
@auth
<form action="/posts/{{ $post->slug }}/comments" method="post"
class="bg-gray-100 border border-gray-200 p-2 rounded-xl">
@csrf
<header class="flex items-center">

<img src="https://i.pravatar.cc/60?u=id{{ $post->author->id }}" width="40"
height="40" class="rounded-full">

<h2 class="ml-3">
Want to Comment?
</h2>
</header>
<div>
<textarea name="body" class="w-full mt-3 rounded-xl" cols="60" rows="3"
required></textarea>
@error('body')
<span class="text-xs text-red-500">{{ $message }}</span>
@enderror
</div>
<button type="submit"
class="text-white uppercase bg-blue-500 font:semibold text-xs py-2 px-10 rounded-2xl flex">Post</button>
</form>
@endauth
@foreach ($post->comments as $comment)
<x-post-comment :comment=$comment />
@endforeach
</section>
</article>
</main>
</section>
</body>
</x-layout>
</x-layout>
17 changes: 10 additions & 7 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\CommentController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\RegisterController;
use App\Http\Controllers\SessionsController;
Expand All @@ -18,12 +19,16 @@
*/

Route::get('/', [PostController::class, 'index'])->name('home');

Route::get('/posts/{post:slug}', [PostController::class, 'show']);
Route::get('/register',[RegisterController::class, 'create'])->middleware('guest');
Route::post('/register',[RegisterController::class, 'store'])->middleware('guest');
Route::get('/logout',[SessionsController::class, 'destroy'])->middleware('auth');
Route::get('/login',[SessionsController::class, 'create']);
Route::post('/login',[SessionsController::class, 'store']);
Route::post('/posts/{post:slug}/comments', [CommentController::class, 'store']);

Route::get('/register', [RegisterController::class, 'create'])->middleware('guest');
Route::post('/register', [RegisterController::class, 'store'])->middleware('guest');

Route::post('/logout', [SessionsController::class, 'destroy'])->middleware('auth');
Route::get('/login', [SessionsController::class, 'create']);
Route::post('/login', [SessionsController::class, 'store']);



Expand All @@ -42,5 +47,3 @@
'categories' => Category::all()
]);
});*/


0 comments on commit 686d5d3

Please sign in to comment.