如何获取具有相同ID的所选文档的当前评论?

时间:2016-08-01 17:53:11

标签: php laravel laravel-5.2

我想获取所选文档的当前评论。但问题是。在我的表评论中,我有相同的<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <table class="table table-bordered grocery-crud-table table-hover"> <tbody> <tr> <td class="warning"> Hey </td> </tr> </tbody> </table>。我只想获得所选文件的评论。

我在想我应该让document_id独一无二吗?我能够得到一个文件的评论。仍然不知道如何检索所选文档的评论。

DB

DocumentController

这是我检索所选文件的评论。正如您在此处所见,我使用 comments 表检索在 sent_document_user 表中选择的文档。但是当我尝试根据所选文档检索评论时。它接受我数据库中的所有注释。我不知道我在哪里收到错误。

document_id

这是用户可以选择的所有列表文档。

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);

}

CommentController

这是保存或插入评论的地方。

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);
}

查看

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();
}
}

模型

注释

<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>

文档

<?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');
    }
}

用户

<?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');
    }
}

移植

文件

<?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');
}
}

用户

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();
    });
}

3 个答案:

答案 0 :(得分:2)

假设

  1. 您的文档中的每个文档都有唯一的ID 模型。即文件表的主键。
  2. 您的用户模型中的每个用户都拥有唯一的ID。即 用户表的主键。
  3. 因此,您的模型将使用以下方法来定义它们与您的其他方法和属性之间的关系。

    文档模型:

    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
    }
    

答案 1 :(得分:1)

如果您想根据所选文档获取所有相关评论,可以通过定义文档模型和评论模型来发挥雄辩的作用。我认为这种关系将是

//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

然后,您将在视图中循环该集合。

答案 2 :(得分:0)

@Francisunoxx我的英语不好但是试着给你参考。你可以按sender_id过滤 假设sender_iduser_id相同 在app/User.php

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

然后在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);
  }

查看resources/views/admin/profile.blade.php

中的个人资料
       @foreach($latest_comments as $cmt)
            <p>{{ $cmt->comment_field }}</p>                
        @endforeach

我希望可以提供帮助