How to get the current comment for selected documents with the same identifier?

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.

DB

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')
        //Table name     //PK                  //FK
        ->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;

        //Obtaining the instance of relationship. The save method will automatically add the appropriate comment_id and sender_id value to the new Comment model.
    $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();
    });
}
+4
source share
3 answers

Assuming that

  • . .
  • . .

, .

:

public function Comments(){
    return $this->hasMany('path_to_your_Comment_Model/Comment');
}

:

//For using in first Approach Below
public function Comments($document_id){
    return $this->hasMany('path_to_your_Comment_Model/Comment')
                ->where('document_id',$document_id);
}
//For using in second Approach Below
public function Comments(){
    return $this->hasMany('path_to_your_Comment_Model/Comment');
}

:

public function User(){
    return $this->belongsTo('path_to_your_User_Model/User');
}

public function Document(){
    return $this->belongsTo('path_to_your_Document_Model/Document');
}

, , document_id, , user_id, .

:

public function getComments(Request $request){
    $document_id = $request->document_id;//Retrieving document_id of selected document  
    $comments = Auth::User->user()->Comments($document_id)->get();
    // Your code to handle retrieved comments
}

:

public function getComments(Request $request){
    $document_id = $request->document_id;//Retrieving document_id of selected document  

    $comments = Auth::User->user()->whereHas('comments',function($query) use($document_id){
                $query->where('document_id',$document_id);
                })
                ->get();
    // Your code to handle retrieved comments
}
+2

, , . ,

//this is in your document model
public function comments() {
    return $this->hasMany(Comment::class);
}

, :

//this is in your controller
$document = Document::find($id);
$document->comments()->get(); //getching all comments
//or
$document->comments; //either will work

.

+1

@Francisunoxx My English is not good, but try to give you a link. you can filter sender_id assuming it sender_idmatches with user_id inapp/User.php

// user has many comments
    public function comments()
    {
        return $this->hasMany('App\CommentsTable','by_user_id');
    }

then in app/Http/Controllers/UserController.php

 public function profile(Request $request, $id) 
  {
    $data['user'] = User::find($id);//find exist user_id

    $data['latest_comments'] = $data['user'] -> comments -> take(5);
    return view('admin.profile', $data);
  }

View profile in resources/views/admin/profile.blade.php

       @foreach($latest_comments as $cmt)
            <p>{{ $cmt->comment_field }}</p>                
        @endforeach

I hope that helps

0
source

Source: https://habr.com/ru/post/1649816/


All Articles