Introduction of Laravel polymorphic association (with code)

01-28-2024

What this article brings to you is the introduction of Laravel polymorphism association (with code attached), which has certain reference value. Friends who need it can refer to it and hope to help you. Laravel polymorphic association (morphTo,morphMany)

In the process of website development, you will often encounter comments on goods, articles, shops, etc. When dealing with such needs, you will often create a new comment table, and then distinguish the comment objects through a type field. The development process is 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'); # id of the comment object $table->text('comment_content'); # Comment content $table->tinyInteger('status'); }); }

Do data migration:

php artisan migrate

Users with data user ids 2 and 4 comment on the goods with product ids 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 stores with store IDs 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'),

After the query data is completed, the next step is to make a query, query all comments with product id of 2, and query the information of reviewers.

public function comment_list(Requset $request, Goods $goods) { # Check all the reviews of goods $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) }

Ordinary linked table lookup

Comment.php file # Comment model file modification # Find information about users who commented. public function member() { return $this->belongsTo(Member::class, 'comment_member_id'); }

Demand query

public function comment_list(Requset $request, Goods $goods) { # Check all the reviews of goods $comments = Comment::where('comment_object_type',Goods::class)->where('comment_object_id',$goods->id)->get(); # save the loop and call $item->member directly to view the user information when the template traverses. dd($comments) }

Polymorphic query

Comment.php file # Comment model file modification # Comment object public function comment_object() { return $this->morphTo(); } # Find information about users who commented. public function member() { return $this->belongsTo(Member::class, 'comment_member_id'); }

Goods.php Document # Commodity Related Review public function comments() { return $this->morphMany(Comment::class,self::class,'comment_object_type','comment_object_id'); }

Demand query

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

This article has all ended here. For more exciting content, please pay attention to the php video tutorial section of PHP Chinese website!

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us