Laravel polymorphism association (morphTo,morphMany)

Laravel polymorphism association (morphTo,morphMany)
In the process of website development, we often encounter comment products, comment articles, comment stores, etc. when dealing with such needs, we often create a new comment table, and then distinguish the object development process of comments through a type field as follows:

New table operation

php artisan make:model Models/Comments -m

Table fields:

 public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('member_id');
            $table->string('comment_object_type');   # Comment object
            $table->integer('comment_object_id');    # The id of the comment object
            $table->text('comment_content');         # Comment content
            $table->tinyInteger('status');
        });
    }

Data migration:

php artisan migrate

Create data
Users with user ID 2 and 4 comment on products with product ID 1, 2, 3 and 4:

INSERT INTO `comments`(`member_id`,`comment_object_type`,`comment_object_id`,`status`,`created_at`,`updated_at`)
VALUES
(2,'App\\Models\\Goods',1,0,'2018-09-07 15:58:04','2018-09-07 15:58:04'),
(2,'App\\Models\\Goods',2,0,'2018-09-07 15:58:04','2018-09-07 15:58:04'),
(2,'App\\Models\\Goods',3,0,'2018-09-07 15:58:04','2018-09-07 15:58:04'),
(2,'App\\Models\\Goods',4,0,'2018-09-07 15:58:04','2018-09-07 15:58:04'),
(4,'App\\Models\\Goods',3,0,'2018-09-07 15:58:04','2018-09-07 15:58:04'),
(3,'App\\Models\\Goods',4,0,'2018-09-07 15:58:04','2018-09-07 15:58:04')

2. The user with user ID 2 commented on the store with store ID 1 and 4

INSERT INTO `comments`(`member_id`,`comment_object_type`,`comment_object_id`,`status`,`created_at`,`updated_at`)
VALUES
(2,'App\\Models\\Stores',1,0,'2018-09-07 15:58:04','2018-09-07 15:58:04'),
(2,'App\\Models\\Stores',4,0,'2018-09-07 15:58:04','2018-09-07 15:58:04'),

query
After the data is created, the next step is to query all the comments with commodity id 2 and find out the information of the reviewer
General query:

public function comment_list(Requset $request, Goods $goods)
  {
        # Query all comments on the product
        $comments = Comment::where('comment_object_type',Goods::class)->where('comment_object_id',$goods->id)->get();
       if($comments) {
          foreach($comments as $comment) {
                 $comment->member = Member::find('id',$comment->member_id)    
           }
       }
       dd($comments)
   }

Common linked table query

Comment.php file

# Comment model file modification
   

    # Find information for users of comments
       public function member()
        {
            return $this->belongsTo(Member::class, 'comment_member_id');
        }

Query of requirements

 public function comment_list(Requset $request, Goods $goods)
  {
        # Query all comments on the product
        $comments = Comment::where('comment_object_type',Goods::class)->where('comment_object_id',$goods->id)->get();
        # It is unnecessary to call $item - > member directly to view user information during template traversal
       dd($comments)
   }

Polymorphic query

Comment.php file

# Comment model file modification
 # Comment object 
   public function comment_object()
    {
        return $this->morphTo();
    }

   # Find information for users of comments
   public function member()
    {
        return $this->belongsTo(Member::class, 'comment_member_id');
    }

Good.php file

# Product related comments
    public function comments()
    {
        return $this->morphMany(Comment::class,self::class,'comment_object_type','comment_object_id');
    }

Query of requirements

public function comment_list(Requset $request, Goods $goods)
 {
        # Query all comments on the product
        $comments =$goods->comments()->with('member')->paginate(15);
        dd($comments)
 }




Tags: PHP Laravel

Posted on Mon, 02 Dec 2019 08:59:47 -0800 by bladx