Laravel limit查询参数结果

Laravel limit查询参数结果,laravel,eloquent,Laravel,Eloquent,我有一个根据用户输入过滤电影的查询,我需要添加一个limit作为查询参数,以限制返回的电影数量,我如何添加这个,我不熟悉编写雄辩的查询 查询 $films = Film ::when($request->input('first'), function ($query, $first) { $query->whereHas('options', function ($query) use ($first) { $query->where('first',

我有一个根据用户输入过滤电影的查询,我需要添加一个
limit
作为查询参数,以限制返回的电影数量,我如何添加这个,我不熟悉编写雄辩的查询

查询

$films = Film
::when($request->input('first'), function ($query, $first) {
    $query->whereHas('options', function ($query) use ($first) {
        $query->where('first', $main);
    });
})
->when($request->input('second'), function ($query, $second) {
    $query->whereHas('options', function ($query) use ($second) {
        $query->where('second', $second );
    });
})
->when($request->input('age'), function ($query, $age) {
    $query->whereHas('ageRatings', function ($query) use ($age) {
        $query->where('age', $age);
    });
})
->when($request->input('country'), function ($query, $country) {
    $query->whereHas('locations', function ($query) use ($country) {
        $query->where('country', $country);
    });
})
->when($request->input('city'), function ($query, $city) {
    $query->whereHas('locations', function ($query) use ($city) {
        $query->where('city', $city);
    });
})
->get();

您可以使用
$query->limit(5)
$query->take(5)

如果要跳过前5个结果,请使用
$query->skip(5)

如果要添加分页,可以使用
$query->paginate(5)


请参阅更多信息:

您可以同时使用这两种方法

极限方法

$limit = 10;
$films = Film
::when($request->input('first'), function ($query, $first) {
    $query->whereHas('options', function ($query) use ($first) {
        $query->where('first', $first);
    });
})
->when($request->input('second'), function ($query, $second) {
    $query->whereHas(‘options', function ($query) use ($second) {
        $query->where('second', $second);
    });
})
->when($request->input('age'), function ($query, $age) {
    $query->whereHas('ageRatings', function ($query) use ($age) {
        $query->where('age', $age);
    });
})
->when($request->input('country'), function ($query, $country) {
    $query->whereHas('locations', function ($query) use ($country) {
        $query->where('country', $country);
    });
})
->when($request->input('city'), function ($query, $city) {
    $query->whereHas('locations', function ($query) use ($city) {
        $query->where('city', $city);
    });
})
->limit($limit)
->get();
$limit = 10;
    $films = Film
    ::when($request->input('first'), function ($query, $first) {
        $query->whereHas('options', function ($query) use ($first) {
            $query->where('first', $first);
        });
    })
    ->when($request->input('second'), function ($query, $second) {
        $query->whereHas('options', function ($query) use ($second) {
            $query->where('second', $second);
        });
    })
    ->when($request->input('age'), function ($query, $age) {
        $query->whereHas('ageRatings', function ($query) use ($age) {
            $query->where('age', $age);
        });
    })
    ->when($request->input('country'), function ($query, $country) {
        $query->whereHas('locations', function ($query) use ($country) {
            $query->where('country', $country);
        });
    })
    ->when($request->input('city'), function ($query, $city) {
        $query->whereHas('locations', function ($query) use ($city) {
            $query->where('city', $city);
        });
    })
    ->take($limit)
    ->get();
采用方法

$limit = 10;
$films = Film
::when($request->input('first'), function ($query, $first) {
    $query->whereHas('options', function ($query) use ($first) {
        $query->where('first', $first);
    });
})
->when($request->input('second'), function ($query, $second) {
    $query->whereHas(‘options', function ($query) use ($second) {
        $query->where('second', $second);
    });
})
->when($request->input('age'), function ($query, $age) {
    $query->whereHas('ageRatings', function ($query) use ($age) {
        $query->where('age', $age);
    });
})
->when($request->input('country'), function ($query, $country) {
    $query->whereHas('locations', function ($query) use ($country) {
        $query->where('country', $country);
    });
})
->when($request->input('city'), function ($query, $city) {
    $query->whereHas('locations', function ($query) use ($city) {
        $query->where('city', $city);
    });
})
->limit($limit)
->get();
$limit = 10;
    $films = Film
    ::when($request->input('first'), function ($query, $first) {
        $query->whereHas('options', function ($query) use ($first) {
            $query->where('first', $first);
        });
    })
    ->when($request->input('second'), function ($query, $second) {
        $query->whereHas('options', function ($query) use ($second) {
            $query->where('second', $second);
        });
    })
    ->when($request->input('age'), function ($query, $age) {
        $query->whereHas('ageRatings', function ($query) use ($age) {
            $query->where('age', $age);
        });
    })
    ->when($request->input('country'), function ($query, $country) {
        $query->whereHas('locations', function ($query) use ($country) {
            $query->where('country', $country);
        });
    })
    ->when($request->input('city'), function ($query, $city) {
        $query->whereHas('locations', function ($query) use ($city) {
            $query->where('city', $city);
        });
    })
    ->take($limit)
    ->get();

你不使用->限制(5)吗?例如bfore->limit(5)->get()?使用
->take(10)->get…
查看其他问题:感谢url的外观,然后
/films?limit=10
在url中获取limit的值你是说你提供的这个解决方案将返回上述url还是我需要获取其他内容,在执行查询之前,您必须首先在url中获取limit的值,并将其初始化为一个变量。关于如何获取要通过的limit值,有两个选项,请访问本文。谢谢,url是这样的吗?limit=5与url无关。您需要更新url或手动从url获取值