laravel查询构建器()何时必须具有多个标签(带有product_tags枢轴表多个对象)

发布于 2025-02-02 22:03:13 字数 1454 浏览 3 评论 0原文

我是Laravel的新手,我有一个复杂的查询要构建。除了用户要求多个标签(tags = 1、2、3)时,我设法对其进行了排序。显示的产品必须具有用户询问的所有标签(不仅是一两个,而且全部)。

我在SQL中有查询(此示例是两个标签,我会根据传递数量的标签将其切换到不同的数字):

SELECT  m.*
FROM  meals m
WHERE EXISTS (
    SELECT 1
    FROM meals_tags t
    WHERE m.id = t.meals_id AND
        t.tags_id IN (227,25)
    HAVING COUNT(1) = 2
);

一个完美工作,但是在将其转换为雄辩的查询构建器时,我有一个问题。其中方法。

我有另一个其中方法中包含在同一查询中,因此我也想附加此方法。

我已经尝试过:

DB::table('meals')
    ->select('id')
    ->where(function ($query) use ($parameters) {
        if (isset($parameters['tags'])) {
            $array = explode(',', $parameters['tags']);
            $query->select(DB::raw(1))
                ->from('meals_tags')
                ->where('meals.id', '=', 'meals_tags.meals_id')
                ->whereIn('meals_tags.tags_id', $array)
                ->having(DB::raw('COUNT(1)'), '=', count($parameters['tags']));
        }
    });

但是我找不到方法。 Laravel和PHP的新手。

假设我有表 和标签 feals_tags将它们连接起来(许多与许多)。


$ paramaters是从get> get(...?tags = 1,2,3& lang = en& enother = eN&emp = somets ...),它们是键值对(['tags'=>'1,2,3','lang'=>'en']

$ parameters ['tags'] 是类型字符串带有逗号分隔数字的字符串(正面Integers大于0),这样我就可以选择在需要时爆炸。

I am new to Laravel and I got a complicated query to build. I managed to sort it except for when a user asks for multiple tags (tags = 1, 2, 3). Product shown has to have all tags that the user asks (not only one or two but all of them).

I have the query in SQL (this example is two tags, I would switch it to different numbers based on how many tags are passed):

SELECT  m.*
FROM  meals m
WHERE EXISTS (
    SELECT 1
    FROM meals_tags t
    WHERE m.id = t.meals_id AND
        t.tags_id IN (227,25)
    HAVING COUNT(1) = 2
);

This one works perfectly, but I have an issue when translating it to an Eloquent query builder where method.

I have another where method included in the same query so I want to attach this one as well.

I have tried this:

DB::table('meals')
    ->select('id')
    ->where(function ($query) use ($parameters) {
        if (isset($parameters['tags'])) {
            $array = explode(',', $parameters['tags']);
            $query->select(DB::raw(1))
                ->from('meals_tags')
                ->where('meals.id', '=', 'meals_tags.meals_id')
                ->whereIn('meals_tags.tags_id', $array)
                ->having(DB::raw('COUNT(1)'), '=', count($parameters['tags']));
        }
    });

But I can't find a way. New to Laravel and PHP.

Let's say I have table meals and tags with meals_tags to connect them (many to many).


$paramaters are comming from GET (...?tags=1,2,3&lang=en&another=something...), they are an array of key-value pairs (['tags' => '1,2,3', 'lang' => 'en'])

$parameters['tags'] is of type string with comma separated numbers (positive integers greater than 0) so that I have the option to explode it if needed somewhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2025-02-09 22:03:13

假设您已经定义了allatsstomany(多次 - 多)feal_tags feal>用餐模型,您可以尝试

Meal::select('id')
    ->when(
        $request->has('tags'),
        function($query) use ($request) {
            $requestedTagIds = explode(',', $request->tags);

            return $query->whereHas(
                'meal_tags', 
                fn ($query) => $query->whereIn('tags_id', $requestedTagIds),
                '=', 
                count($requestedTagIds)
            );
        }
    )
    ->get();

Assuming that you have defined belongsToMany (Many-to-Many) meal_tags relationship on the Meal model, you can try

Meal::select('id')
    ->when(
        $request->has('tags'),
        function($query) use ($request) {
            $requestedTagIds = explode(',', $request->tags);

            return $query->whereHas(
                'meal_tags', 
                fn ($query) => $query->whereIn('tags_id', $requestedTagIds),
                '=', 
                count($requestedTagIds)
            );
        }
    )
    ->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文