Php 多动态查询范围

Php 多动态查询范围,php,laravel,Php,Laravel,我已经在我的数据库查询上构建了一个过滤器,现在 我想让它更舒适。 目标是使其动态化,这意味着 用户可以选择合并任意数量的内容 public function filter(FilterList $request) { $view = view('profile.browse.list'); $search = $request->get('filter'); $filter = [ 'male' => Profile::male(

我已经在我的数据库查询上构建了一个过滤器,现在 我想让它更舒适。 目标是使其动态化,这意味着 用户可以选择合并任意数量的内容

public function filter(FilterList $request)
{
    $view = view('profile.browse.list');
    $search = $request->get('filter');

    $filter = [
            'male'   => Profile::male(),
            'female' => Profile::female(),
            '10'     => Profile::man(),
            '11'     => Profile::women(),
            '12'     => Profile::couple(),
            '15'     => Profile::domina(),
            '20'     => Profile::affaire(),
            '21'     => Profile::cybersex(),
            '22'     => Profile::marriage(),
            '23'     => Profile::flirt(),
    ];

    return $view->with([
        'List' => $filter[$search]->unmoderated()->paginate(15)->appends(['filter' => $search]),
    ]);

}
我知道我的解决方案并不是一个好的解决方案。 我已将查询范围保存在$filter数组中,并且 不能像这样添加第二个或第三个作用域

$users=User::popular()->women()->orderBy('created_at')->get()

有什么提示吗?这是给我的,让它变得更好

我不知道该怎么开始

谢谢你的帮助

编辑

public function scopeMarriage($query)
{
 return $query->where('LookFor', 'LIKE', '%Ehe%');
}

因此,用户正在提交动态数量的输入,并且基于这些输入,您希望继续将查询范围链接到您的查询上吗?如果这是你想要的,我相信你可以这样做:

// Method array taken from your example
$methods = [
    'male'    => 'male',
    'female'  => 'female',
    '12'      => 'couple',
];

// Begin the query. Since the unmoderated() scope query is always called,
// I used that in this example.
// But note that you can also use Model::query(); to begin a query
$query = Profile::unmoderated();

// Loop through the request inputs
foreach ($request->all() as $param)
{
    // If it exists in the provided array, add it to the query.
    if (array_key_exists($param, $methods))
    {
        $query->{$methods[$param]}();
    }
}

// Call get, paginate, etc. to actually fetch the results and then return it.
return $query->paginate(15);

您需要创建一个类来注册过滤器并将其应用于
user
querybuilder。等我有时间的时候,也许我会去的。示波器是什么样子的?你能补充一个问题吗?同时,我有一个解决问题的办法。是否可以在循环中创建methode调用?也许像这样…>但请注意,您也可以使用Model::query();开始一个问题,这一行帮助了我。非常感谢你。