如何根据查询的结果执行/返回查询的一部分(使用Eloquent或原始SQL)


How to execute/return a part of a query based on a result of that query (with Eloquent, or raw SQL)?

我试图根据页面的段塞(标识符)和设置区域设置/语言来获取页面的内容。但是,如果所选语言环境的页面在数据库中不可用,我希望返回具有后备语言环境的页面,该语言环境应该始终可用(如果不是,则返回错误)。

对于我的应用程序,我使用Laravel的Eloquent ORM。

'pages'表中的数据(伪代码):

- entry 1:
    title: About
    slug: about
    locale: en
    content: the content of the about page
- entry 2:
    title: Informatie
    slug: about
    locale: nl
    content: De tekst van de informatie pagina
- entry 3:
    title: Home
    slug: home
    locale: en
    content: The content of the home page

期望输出(带回退区域设置= en):

Required return to set $page to:
if the selected locale = nl, slug = about:
     'entry 2'
if the selected locale = en, slug = about:
     'entry 1'
if the selected locale = nl, slug = home:
     (since the nl-page is not available for this slug)
     set $page to 'entry 3'

这是我写的代码:

<?php
... other code ...
//based on selection
$slug = 'something';
$locale = 'the selected locale';
//setting for fallback Locale
$fallbackLocale = 'en';
//first try to get the page with the selected locale        
$page = Page::where([
                    'slug' => $slug,
                    'locale' => $locale
                ])
                ->first();
// if the first query is empty, get the page with the fallback Locale
if (empty($page))
{
    $page = Page::where([
                        'slug' => $slug,
                        'locale' => $fallbackLocale
                    ])
                    ->firstOrFail();
}

可以看到,虽然我执行了两个查询,但这段代码确实可以工作。我想执行一个查询,它检查查询的前半部分是否返回一些东西(具有所选语言环境的页面),如果这是空的,那么查找具有后退语言环境的页面(段符仍然相同)。

是否有办法做到这一点与雄辩?(我不这么认为,在雄辩中使用'if'语句的方法是用来检查是否设置了表单中的参数,而不是查询返回的东西)

如果这是不可能的雄辩,是可能的,只是普通的SQL?

如果您同时选择并按locale = 'en'排序,'nl'将首先被选中,因为'nl' = 'en'0, 'en' = 'en'1

$page = Page::where('slug', $slug)
    ->whereIn('locale', [$locale, $fallbackLocale])
    ->orderByRaw("locale = '$fallbackLocale'")
    ->first();

这里的问题:如果$fallbackLocale来自用户输入,它不是注入保存。我没有找到在orderByRaw()原因中使用占位符的方法。

但是您可以使用带有占位符的原始查询:

$page = Page::hydrateRaw("
        select *
        from pages
        where slug = ?
          and locale in (?, ?)
        order by locale = ?
        limit 1
    ",
    [$slug, $locale, $fallbackLocale, $fallbackLocale]
)->first();

更新:

我找到了一种在orderByRaw()中使用占位符的方法:

$page = Page::where('slug', $slug)
    ->whereIn('locale', [$locale, $fallbackLocale])
    ->orderByRaw("locale = ?")
    ->addBinding($fallbackLocale, 'order')
    ->first();