Business Logic Processing of Laravel Point Comment and Comment Module

Requirement: A user gives or cancels a comment on an article and then returns to the list page of the article to show the number of comments and the number of comments.

In laravel, we can use the idea of model association to develop this function more quickly and clearly.

First of all, I will introduce the relationship between the commentary tables and the compliment tables in the following chapters.

1. Table structure

Comment Table (Model Layer Name Comment)

Poss (Model Layer Name Post)

zans (Model Layer Name Zan)

2. Model association between tables

First, Comment.php, the model of the comment table

Multiple comments act on an article and are therefore reverse many-to-one, or belongsto

More than one comment comes from the same user and is therefore a reverse many-to-one, belongsto

So establish the following relationships


namespace App;

use Illuminate\Database\Eloquent\Model as BaseModel;

class Comment extends BaseModel
    //One-to-many reverse comments belong to articles
    public function post(){
        return $this->belongsTo('App\Post','post_id','id');

    //One-to-many reverse comments belong to users
    public function user(){
        return $this->belongsTo('App\user','user_id','id');


Next up is the article model layer Post.php

Multiple articles come from the same user, so it's a reverse one-to-many, or belongsTo

There are many comments in an article, so it's a positive one-to-many, that is hasMany.

There are many compliments in an article, so it's a positive one-to-many, that is hasMany.

A user can only praise one article, so one-to-one is hasOne.

So establish the following relationships


namespace App;
 * Articles Model
use App\Model;
class Post extends Model
    protected $table = "posts";
    protected $fillable = ['title','content','user_id'];

    //Associated user articles belong to user_id of user articles associated with user's ID
    public function user(){
        return $this->belongsTo('App\User','user_id','id');

    //There are multiple reviews in the article. One-to-many access to all reviews of the article is shown back by publishing time.
    public function comments(){
        return $this->hasMany('App\Comment','post_id','id')->orderBy('created_at','desc');

    //    A user can only judge a user's approval of an article by associating it with an article.
    public function zan($user_id){
        return $this->hasOne('App\Zan')->where('user_id',$user_id);
    //Getting all the compliments of an article has more than one compliment.
    public function zans(){
        return   $this->hasMany('App\Zan');


That's all for the relevance of the model.

Next, write a way to praise and cancel praise.

These two methods are written directly in the article controller.

The first is the logic of praise.

Because of the design of the table, you just need to set the id of the article where the id of the user who inserts the praise is praised.

Then call the method firstOrCreate that comes with the model layer, which means to return this data if it exists or insert a data if it does not exist.

//Generate praise
    public function zan(Post $post){

        //Processing parameters
        $params = [
            'user_id'=> Auth::id(),
        //Receive parameters for business processing, if not create presence, look up
        return back();

Then comes the logic of abolishing praise.

Canceling approval is based on whether the user id of the operation exists or not.

Call the method of the model layer above to delete the returned data and cancel the praise.

//Cancel praise
    public function unzan(Post $post){
        return back();

After that, the template page treats the peer-to-peer praise as follows: If the peer praise is displayed, the peer praise is cancelled, if the peer praise is not displayed, the peer praise button is displayed.

{{--                Determine if the user has already clicked the praise--}}
                    <a href="/posts/{{$post->id}}/unzan" type="button" class="btn  btn-default">Cancel praise</a>
                    <a href="/posts/{{$post->id}}/zan" type="button" class="btn  btn-primary">Fabulous</a>

All in all, the point of approval and cancellation of approval is actually repeated insertion and deletion operations!

Logical Implementation of Comments

Needless to say, submit in form

Because the article model and the comment model are related, we can write the logic in the control of the article just like the function of dealing with the praise above.

//Cancel praise
    public function unzan(Post $post){
        return back();

The last part is to show how many points of praise and pinglunshu each article has on the list page of articles.

First of all, these processing must be done in the controller method of the article list.

Here we use a function withCount that comes with the framework.

$posts = Post::orderBy('created_at','desc')->withCount(['comments','zans'])->paginate(15);

Because we configure the model association, we only need the comment sheet with Count here to get the comment number and comment number for each article.

How do template pages display these quantities?

The format is the object - > - > indicating that _count can be rendered!

 <p class="blog-post-meta">Fabulous {{$post->zans_count}}| comment{{$post->comments_count}}</p>

The end result is this.

Here's the logic of this implementation. Let's praise it for its usefulness.


Keywords: PHP Laravel Database

Added by Madzz on Fri, 30 Aug 2019 12:59:30 +0300