laravel评论列表模糊搜索及优化

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

一、评价管理列表api

1.1 创建评价表模型及迁移文件

运行命令:php artisan make:model Comment -m 在这里插入图片描述

1.2 完成评论表字段

Schema::create('comments', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->comment('评论的用户');
            $table->integer('goods_id')->comment('所属商品');
            $table->integer('rate')->default(1)->comment('评论的类别:1->好评,2->中评,3->差评');
            $table->string('content')->comment('评论的内容');
            $table->string('reply')->nullable()->comment('商家的回复');
            $table->json('pics')->nullable()->comment('多个评论图');
            $table->timestamps();
        });
复制代码

在这里插入图片描述

执行迁移命令:php artisan migrate生成迁移表 在这里插入图片描述

1.3 创建评论控制器

创建评论资源控制器用来处理评论相关的业务逻辑: 运行命令:php artisan make:controller Admin/CommentController --api 在这里插入图片描述

1.4 创建评价相关路由

            /*
             * 评价管理
             */
            // 评价列表
            $api->get('comments', [CommentController::class, 'index']);

            // 评价详情
            $api->get('comments/{comment}', [CommentController::class, 'show']);

            // 回复评价
            $api->get('comments/{comment}/reply', [CommentController::class, 'reply']);
复制代码

在这里插入图片描述 因为这里的评价是由用户端去添加的,我们这边手动往数据库中添加一些数据,进行测试。 在这里插入图片描述

1.5 创建评价transformer

创建评价CommentTransformer.php写入如下格式化代码:

<?php 

namespace App\Transformers;

use App\Models\Comment;
use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract {

    public function transform(Comment $comment) {
        return [
            'id' => $comment->id,
            'content' => $comment->content,
            'user_id' => $comment->user_id,
            'goods_id' => $comment->goods_id,
            'rate' => $comment->rate,
            'reply' => $comment->reply,
            'updated_at' => $comment->updated_at,
            'created_at' => $comment->created_at,
        ];
    }

}
复制代码

在这里插入图片描述

1.6 评价列表控制器方法

    /**
     * 评价列表
     */
    public function index(Request $request)
    {
        // 获取搜索条件
        $rate = $request->query('rate');

        // 商品的名称模糊搜索评价
        $goods_title = $request->query('goods_title');

        $comments = Comment::when($rate, function ($query) use ($rate) {
                        $query->where('rate', $rate);
                    })
                    -> when($goods_title, function ($query) use ($goods_title){
                        // 先查询相关的商品id
                        $goods_ids = Good::where('title', 'like', "%{$goods_title}%")->pluck('id');
                        $query->whereIn('goods_id', $goods_ids);
                    })
                    ->paginate(1);
        return $this->response->paginator($comments, new CommentTransformer());
    }

复制代码

在这里插入图片描述

rate管理员可以通过搜索好评中评差评来搜索对应的数据 goods_title管理员可以通过搜索商品的名称进行模糊搜索对应数据

1.7 测试

在这里插入图片描述 在这里插入图片描述

二、优化

从以上的测试中我们可以知道,当前有了评价,但是我们还想知道是哪个用户评价的,以及评价的是哪个商品,所以接下来修改评价的transformer: 定义一个受保护的属性,可以让它引入一些内容:

    // 可include的方法
    protected $availableIncludes = ['user', 'goods'];
    // 用户数据
    public function includeUser(Comment $comment) {
        return $this->item($comment->user, new UserTransformer());
    }

    // 商品数据
    public function includeGoods(Comment $comment) {
        return $this->item($comment->goods, new GoodTransformer());
    }
复制代码

在这里插入图片描述


修改评价模型进行对用户的关联:

    /**
     * 评论所属用户
     */
    public function user() {
        // 评价是属于用户
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    /**
     * 评论所属商品
     */
    public function goods() {
        // 评价是属于用户
        return $this->belongsTo(Good::class, 'goods_id', 'id');
    }
复制代码

在这里插入图片描述


测试结果: 在这里插入图片描述 在这里插入图片描述 但是评论如果用户有上传多图的话,那么也是一样的处理,我们加上对多图的处理模拟数据: 在这里插入图片描述 同样这边多图是json类型的我们同样在评论模型中加上强制类型转换: 在这里插入图片描述

CommentTransformer.php加上:

$pic_urls = [];

        if (is_array($comment->pics)) { // 因为图不是必填的,做判断防止报错
            foreach($comment->pics as $p) {
                array_push($pic_urls, oss_url($p));
            }
        }
复制代码

在这里插入图片描述

效果: 在这里插入图片描述

在学习的php的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。

猜你喜欢

转载自juejin.im/post/7016582716053274638