gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:17:03 26 4
gpt4 key购买 nike

我想获取所选文档的当前评论。但问题是。在我的表格 comments 中,我有相同的 document_id。我只想获取所选文档的评论。

我在想我是否应该让我的 document_id 独一无二?对我来说,能够获得一份文件的评论。仍然不知道如何检索所选文档的评论。

DB

文档 Controller :

这是我检索所选文档和评论的地方。正如您在这里看到的,我检索了在我的 sent_document_user 表中选择的文档以及 comments 表。但是当我试图根据选定的文档检索评论时。它需要我数据库中的所有评论。我不知道哪里出错了。

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

}

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

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

评论 Controller

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

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

用户

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

最佳答案

假设

  1. 您的文档中的每个文档都有唯一的 ID模型。即文档表的主键。
  2. 您的用户模型中的每个用户都有一个唯一的 ID。 IE。用户表的主键。

因此,您的模型将具有以下方法来定义它们之间的关系以及您的其他方法和属性。

文档模型:

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 标识的用户的评论表中检索评论,您可以在 Controller 中使用以下代码。

第一种方法:

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
}

关于php - 如何获取具有相同 ID 的选定文档的当前评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38705175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com