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

标签 php mysql sql eloquent

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

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

“页面”表中的数据(伪代码):

- 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

期望的输出(使用 fallback locale = 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();
}

如您所见,虽然我正在执行两个查询,但这段代码确实有效。我想执行一个查询,它检查查询的前半部分是否返回某些内容(具有所选语言环境的页面),如果这是空的,则查找具有后备语言环境的页面(slug 仍然是相同的)。

有没有办法用 Eloquent 做到这一点?(我不这么认为,Eloquent 中带有“if”语句的方法是用来检查是否设置了表单中的参数,而不是检查查询是否返回了某些东西)

如果用 Eloquent 做不到,那么只用普通的 SQL 就可以吗?

最佳答案

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

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

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

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

$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();

关于php - 如何根据查询的结果执行/返回查询的一部分(使用 Eloquent 或原始 SQL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317475/

相关文章:

sql - Spark SQL查询与DataFrame函数

sql - 无法引用联合之外的列

javascript - php 生成 html 按钮来运行 javascript

MYSQL查询: Select 1 rows photo from table for each album

php - 解释 PHP 中引号内的 PHP

php - 当没有发生数据库异常时,Laravel 事务不会回滚

Windows 8.1 Pro 64 位上的 MySQL 5.6.14 64 位连接问题

sql - Microsoft SQL Server 数据工具包未正确加载

javascript - JS 文件无法正常工作 CodeIgniter

php - 我在哪里可以让 PHP 安装在 Windows 服务器 (x64) 上