首页 > 解决方案 > Laravel Eloquent 返回 null

问题描述

我有 laravel 的多语言网站。我正在为参数传递slug。从 berita 传递到 beritaDetail。但它显示错误,当 dd($berita) 时,只显示 null

$title = "title_".App::getLocale()." as title";
$content = "content_".App::getLocale()." as content";
$slug = "slug_".App::getLocale();
$berita = Report::select("*", "$title", "$content")->where('slug_'.App::getLocale(), $slug)->first();

dd($berita);
$beritaRandom = Report::take(6)->inRandomOrder()->get();

return view('frontend.pages.berita_detail', array('berita' => $berita,
'beritaRandom' => $beritaRandom ));

我希望输出是 $berita 有一个数组,其中包含与 slug 匹配的字段而不是 null 或空数组

标签: phplaravelmultilinguallaravel-5.7

解决方案


$slug = "slug_".App::getLocale();

$berita = Report::select("*", "$title", "$content")->where('slug_'.App::getLocale(), $slug)->first();

您在这里所做的实际上是检查 slug_LOCALE 是否等于自身。因此倾倒时预计会为空$berita

祝你好运


推荐阅读