php – 如何获取具有相同ID的所选文档的当前评论?
发布时间:2020-12-13 17:01:04 所属栏目:PHP教程 来源:网络整理
导读:我想获得所选文档的当前评论.但问题是.在我的表评论中,我有相同的document_id.我只想获得所选文件的评论. 我想我应该使我的document_id独一无二吗?我能够得到一个文件的评论.仍然不知道如何检索所选文档的评论. DocumentController: 这是我检索所选文档的
我想获得所选文档的当前评论.但问题是.在我的表评论中,我有相同的document_id.我只想获得所选文件的评论.
我想我应该使我的document_id独一无二吗?我能够得到一个文件的评论.仍然不知道如何检索所选文档的评论. DocumentController: 这是我检索所选文档的注释.正如您在这里看到的,我检索了带有comments表的sent_document_user表中选择的文档.但是当我尝试根据所选文档检索评论时.它接受我数据库中的所有注释.我不知道我在哪里得到错误. 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','receive_document_user.dateReceived','documents.id') //Table name //PK //FK ->join('users','receive_document_user.user_id') ->join('documents','receive_document_user.document_id') ->join('categories','documents.category_id') ->where('sender_id',Auth::id())->get(); return view ('document.sent')->with('documentSent',$documentSent); } CommentController 这是保存或插入注释的地方. 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 AppModels; use IlluminateDatabaseEloquentModel; class Comment extends Model { protected $tables = 'comments'; protected $fillable = [ 'comment_content',]; public function documents() { return $this->belongsTo('AppModelsDocument'); } public function users() { return $this->belongsTo('AppModelsComment'); } } 文献 <?php namespace AppModels; use IlluminateDatabaseEloquentModel; class Document extends Model { protected $table = 'documents'; protected $fillable = [ 'title','content','category_id',]; public function comments() { return $this->hasMany('AppModelsComment'); } } 用户 <?php namespace AppModels; use IlluminateAuthAuthenticatable; use IlluminateDatabaseEloquentModel; use IlluminateContractsAuthAuthenticatable as AuthenticatableContract; class User extends Model implements AuthenticatableContract { use Authenticatable; public function comments() { return $this->hasMany('AppModelsComment'); } } 移民 文件 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(); }); } 解决方法
假如说
>您的文档中的每个文档都有唯一的ID 因此,您的模型将使用以下方法来定义它们与您的其他方法和属性之间的关系. 文件型号: 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标识的Document和user_id标识的User的comments表中检索Comments,可以在控制器中使用以下代码. 第一种方法: 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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |