I wanted to get the current comment for the selected document. But that is the problem. I have the same comments in my tables document_id. I just wanted to get a comment on the selected documents.
I think I should make my document_idunique? For me, the opportunity to get a comment on one document. Still don't know how I can get a comment for the selected documents.

DocumentController
Here I retrieve a document that also selects a comment. As you can see here, I am retrieving the document that was selected in my sent_document_user table with the comments table . But when I tried to get comments based on the selected documents. It accepts all comments in my database. I do not know where I get the error.
public function readSentDocuments($id)
{
$documentLists = DB::table('sent_document_user')->select('documents.title', 'categories.category_type', 'documents.content', 'documents.id')
->join('documents', 'documents.id', '=', 'sent_document_user.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')->first();
$commentLists = DB::table('comments')
->select('comments.comment_content', 'users.username', 'comments.id')
->join('users', 'users.id', '=', 'comments.commentBy')->get();
return view ('document.readSent')->with('documentLists', $documentLists)->with('commentLists', $commentLists);
}
This is the entire list of documents in which the user has the opportunity to choose.
public function showSentDocuments()
{
$documentSent = DB::table('receive_document_user')->select('documents.title', 'documents.content', 'categories.category_type', 'users.username', 'receive_document_user.dateReceived', 'documents.id')
->join('users', 'users.id', '=', 'receive_document_user.user_id')
->join('documents', 'documents.id', '=', 'receive_document_user.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->where('sender_id', '=', Auth::id())->get();
return view ('document.sent')->with('documentSent', $documentSent);
}
CommentController
This is where you save or insert comments.
class CommentController extends Controller
{
public function postComments(Request $request, Document $id)
{
$this->validate($request,
[
'comment' => 'required',
]);
$commentObject = new Comment();
$user = Auth::user();
$commentObject->comment = $request->comment;
$commentObject->sender_id = $user->id;
$id->comments()->save($commentObject);
return redirect()->back();
}
}
View
<div class = "col-md-6">
<form class = "form-vertical">
<div class = "form-group">
<div class="panel panel-default">
<div class="panel-heading">Comments</div>
@foreach ($commentLists as $list)
<div class="panel-body">
<p>{{ $list->comment }}</p>
<strong>Comment by:</strong>
<p>{{ $list->username }}</p>
<button type = "submit" class = "btn btn-primary">Reply</button>
</div>
@endforeach
</div>
</div>
</form>
</div>
Models
Comment
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $tables = 'comments';
protected $fillable =
[
'comment_content',
];
public function documents()
{
return $this->belongsTo('App\Models\Document');
}
public function users()
{
return $this->belongsTo('App\Models\Comment');
}
}
Document
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Document extends Model
{
protected $table = 'documents';
protected $fillable =
[
'title',
'content',
'category_id',
];
public function comments()
{
return $this->hasMany('App\Models\Comment');
}
}
User
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Model implements AuthenticatableContract
{
use Authenticatable;
public function comments()
{
return $this->hasMany('App\Models\Comment');
}
}
Migration
documents
public function up()
{
Schema::create('documents', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('content');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
of users
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('middle_name');
$table->string('email');
$table->string('username');
$table->string('address');
$table->string('password');
$table->string('remember_token');
$table->integer('role_permission_id')->unsigned();
$table->foreign('role_permission_id')->references('id')->on('roles_permissions_dt')->onDelete('cascade');
$table->timestamps();
});
}