Php 如何基于查询结果执行/返回查询的一部分(使用雄辩的SQL或原始SQL)?

Php 如何基于查询结果执行/返回查询的一部分(使用雄辩的SQL或原始SQL)?,php,mysql,sql,eloquent,Php,Mysql,Sql,Eloquent,我试图根据页面的slug(标识符)和设置的区域设置/语言来获取页面内容。但是,如果页面在具有所选区域设置的数据库中不可用,我希望返回具有备用区域设置的页面,该区域设置应始终可用(如果不可用,则返回错误) 对于我的应用程序,我使用的是Laravel雄辩的ORM “页面”表中的数据(伪代码): 所需输出(回退区域设置为en): 这是我写的代码: <?php ... other code ... //based on selection $slug = 'something'; $locale

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

对于我的应用程序,我使用的是Laravel雄辩的ORM

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

所需输出(回退区域设置为en):

这是我写的代码:

<?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();
}

如果您同时选择和order by
locale='en'
将首先选择'nl',因为
'nl'='en'
0
'en'='en'
1

$page=page::where('slug',$slug)
->其中('locale',[$locale,$fallbackLocale])
->orderByRaw(“语言环境=”$fallbackLocale'))
->第一个();
这里的问题是:如果
$fallbackLocale
来自用户输入,则不是注入保存。我没有找到在
orderByRaw()
caluse中使用占位符的方法

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

$page=page::hydrateRaw(“
挑选*
从页数
其中slug=?
以及(?,)中的区域设置
按区域设置排序=?
限制1
",
[$slug,$locale,$fallbackLocale,$fallbackLocale]
)->第一个();
更新:

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

$page=page::where('slug',$slug)
->其中('locale',[$locale,$fallbackLocale])
->orderByRaw(“语言环境=?”)
->addBinding($fallbackLocale,'order')
->第一个();

示例数据和所需结果将真正有助于传达您想要做的事情。您如何知道一个查询会更快?现在有多慢?这会影响性能吗?你量过了吗?您使用一个数据查询进行了测试?速度快吗?依我看,你需要测量它。如果没有测量,你如何决定哪一个更好或更差?没有证据表明一个查询总是或经常优于两个查询。为什么?第一个查询将内容加载到内存中……好吧,我不知道如何用一个查询来完成,所以我无法用一个查询来度量它。我认为通常只执行一个查询比执行多个查询更好,因为只需要建立一个连接(尽管多个较小的查询可能比一个较大的查询更快)。我很喜欢你的想法。尽管当我实现第一个方法时,它给了我一个错误,它不知道'en'列:SQLSTATE[42S22]:未找到列:1054'order子句'中的未知列'en'(SQL:select*from
pages
where
slug
=home和
locale
=en)@FeatherNL-修复了这个问题。但是,除非找到使用占位符的方法,否则不应使用第一个解决方案。
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();
}