如何在Lighthouse-php中编写此查询? -> Blog :: where'status',1-> where'title','like','%'$ term'%'-> paginate6

问题描述

我在Meta项目中使用类似的查询获取数据:

Laravel

现在我将-> Blog::where('status',1)->where('title','like','%'.$term.'%')->paginate(6); 用于我的后端API。

如何在Laravel+Lighthouse-PHP中编写此查询

解决方法

您可以在模型中使用雄辩性范围来确定您的状态条件:

public function scopeEnabledBlogs($query) {
    return $query->where('status',1);
}

并在您的架构中使用:

extend type Query {
    Blogs(title: String @where(operator: "like")): [Blog!] @paginate(scopes: ["enabledBlogs"])
}

现在,您可以查询博客,例如:

query {
    Blogs(first: 6 title: "%something%") {
        data {
            title
        }
    }
}