首页 >php框架 >Laravel > 正文

Laravel多态关联的介绍(附代码)

转载2019-03-22 17:42:0401939
本篇文章给大家带来的内容是关于Laravel多态关联的介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

Laravel 多态关联(morphTo,morphMany)

在网站开发的过程中,经常会遇到 评论商品,评论文章, 评论店铺 等等,在处理这样的需求的时候, 经常会新建一张 评论表, 然后通过 一个 type字段来区分 评论的对象 开发过程如下:

新建表操作

php artisan make:model Models/Comments -m

表字段:

 public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('member_id');
            $table->string('comment_object_type');   # 评论对象
            $table->integer('comment_object_id');    # 评论对象的id
            $table->text('comment_content');         # 评论内容
            $table->tinyInteger('status');
        });
    }

做数据迁移:

php artisan migrate

造数据
用户 ID 为2和4 的用户对 商品ID 为 1,2,3,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.用户ID 为2 的用户 对 店铺ID 为 1,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'),

查询
数据造完毕, 接下来要做查询,查询一下 商品id为2的 所有评论, 并且查询出评论人的信息
普通查询:

public function comment_list(Requset $request, Goods $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)
   }

普通连表查

Comment.php 文件
# Comment model 文件修改
   

    # 查找评论的用户的信息
       public function member()
        {
            return $this->belongsTo(Member::class, 'comment_member_id');
        }

需求的查询

 public function comment_list(Requset $request, Goods $goods)
  {
        # 查询商品的所有评论
        $comments = Comment::where('comment_object_type',Goods::class)->where('comment_object_id',$goods->id)->get();
        # 省掉了 循环 在模板遍历的时候 直接调用  $item->member 查看用户信息
       dd($comments)
   }

多态查询

Comment.php 文件
# Comment model 文件修改
 # 评论对象 
   public function comment_object()
    {
        return $this->morphTo();
    }

   # 查找评论的用户的信息
   public function member()
    {
        return $this->belongsTo(Member::class, 'comment_member_id');
    }
Goods.php 文件
# 商品关联评论
    public function comments()
    {
        return $this->morphMany(Comment::class,self::class,'comment_object_type','comment_object_id');
    }

需求的查询

public function comment_list(Requset $request, Goods $goods)
 {
        # 查询商品的所有评论
        $comments =$goods->comments()->with('member')->paginate(15);
        dd($comments)
 }

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的php视频教程栏目!

以上就是Laravel多态关联的介绍(附代码)的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文转载于:segmentfault,如有侵犯,请联系admin@php.cn删除

  • 相关标签:php laravel
  • 相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • 专题推荐

    作者信息
    推荐视频教程
  • Laravel5.4电商实战视频教程Laravel5.4电商实战视频教程
  • laravel基础开发宝典视频教程laravel基础开发宝典视频教程
  • Laravel开发商城前台视频教程Laravel开发商城前台视频教程
  • 最新laravel商城实战视频教程最新laravel商城实战视频教程
  • Laravel电商项目实战课程Laravel电商项目实战课程
  • 视频教程分类