Reputation: 604
Basically, I want to show a posts comment.
Whatt is supposed to happen is that,when a user goes to view a post using a url such as http://localhost:8000/post/show/1
,their supposed to be able to to type a comment on a post,press 'submit' ,the comment data inserted into the database,the page should reload and the comments on that post should be shown including the comment just commented by the user
BUT
what currently happens is that after typing the comment in http://localhost:8000/post/show/1
it inserts the comment data into the database,reloads the page but does not show any comment on that page.
I have no clue what I'm doing wrong.
Please help
These are my code:
PostController.php
<?php
// PostController.php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;
class PostController extends Controller
{
protected $fillable = [
'Uploader',
];
public function __construct()
{
return $this->middleware('auth');
}
public function create()
{
return view('post');
}
public function store(Request $request)
{
{
$post = new Post;
$post->title = $request->get('title');
$post->type = $request->get('type');
$post->description = $request->get('description');
$post->body = $request->get('body');
$post->UniqueId = str_random(16);
$post->Uploader = Auth::user()->name;
$post->Language = 'en';
$post->Location=Location::get()->countryCode;
$post->views = 0;
$post->Applauds = 0;
$post->Boos = 0;
$post->Tags = "hey";
if ($request->get('agegroup')) {
$post->agegroup = $request->get('agegroup');
} else {
$post->agegroup ='undefined';
}
$post->AllowComments = 'true';
$post->CommentsBg = 'default';
$post->Visibility = 'globally public';
$post->others = 'embeddable';
$post->save();
return redirect('posts');
}
}
public function index()
{
$posts = Post::all();
return view('index', compact('posts'));
}
public function show($id)
{
$post = Post::find($id);
return view('show', compact('post'));
}
}
CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\Post;
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->get('comment_body');
$comment->user()->associate($request->user());
$post = Post::find($request->get('post_id'));
$post->comments()->save($comment);
return back();
}
}
Web.php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/upload', 'UploadController@index')->name('upload');
Route::get('/post/create', 'PostController@create')->name('post.create');
Route::post('/post/store', 'PostController@store')->name('post.store');
Route::get('/posts', 'PostController@index')->name('posts');
Route::get('/post/show/{id}', 'PostController@show')->name('post.show');
Route::post('/comment/store', 'CommentController@store')->name('comment.add');
Route::post('/reply/store', 'CommentController@replyStore')->name('reply.add');
Route::match(['get', 'post'], 'imageupload', 'ImageController@Image');
Route::delete('delimage/{filename}', 'ImageController@Image');
post.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Post</div>
<div class="card-body">
<form method="post" action="{{ route('post.store') }}">
<div class="form-group">
@csrf
<label class="label">Post Title: </label>
<input type="text" name="title" class="form-control" required/>
</div>
<label class="label">Post Type </label>
<input type="text" name="type" class="form-control" required/>
<label class="label">Tags </label>
<input type="text" name="tags" class="form-control" required/>
<label class="label">Age-group(optional) </label>
<input type="text" name="agegroup" class="form-control" required/>
<div class="form-group">
<label class="label">Post Description </label>
<textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
</div>
<div class="form-group">
<label class="label">Post Body: </label>
<textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
show.blade.php
<!-- show.blade.php -->
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<p><b>{{ $post->title }}</b></p>
<p>
{{ $post->body }}
</p>
<hr />
<h4>Display Comments</h4>
@foreach($post->comments as $comment)
<div class="display-comment">
<strong>{{ $comment->user->name }}</strong>
<p>{{ $comment->body }}</p>
</div>
@endforeach
<hr />
<h4>Add comment</h4>
<form method="post" action="{{ route('comment.add') }}">
@csrf
<div class="form-group">
<input type="text" name="comment_body" class="form-control" />
<input type="hidden" name="post_id" value="{{ $post->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
index.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Title</th>
<th>Action</th>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{ $post->Id }}</td>
<td>{{ $post->title }}</td>
<td>
<a href="{{ route('post.show', ['id' => $post]) }}" class="btn btn-primary">Show Post</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@endsection
Post.php
<?php
// Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable')->whereNull('parent_id');
}
}
Comment.php
<?php
// Comment.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
}
DBSCHEMA for comments table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
// create_comments_table
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('parent_id')->unsigned();
$table->integer('applauds')->unsigned();
$table->integer('boos')->unsigned();
$table->text('body');
$table->integer('commentable_id')->unsigned();
$table->string('commentable_type');
$table->string('No of flags');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Upvotes: 0
Views: 72
Reputation: 550
Update the PostController
function as below:
public function show($id)
{
$post = Post::where('id', $id)->with('comments.user')->first();
return view('show', compact('post'));
}
It should get comments and user details attached
On the Post
Model, update comments
function
public function comments()
{
return $this->hasMany(Comment::class, 'parent_id');
}
Upvotes: 1