Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 嵌套的stdClass不';t返回预期结果_Php_Oop_Laravel 5 - Fatal编程技术网

Php 嵌套的stdClass不';t返回预期结果

Php 嵌套的stdClass不';t返回预期结果,php,oop,laravel-5,Php,Oop,Laravel 5,在Laravel5.1中,我在Article.php模型上创建了上述函数。其想法是,它将检查每种提供的语言是否存在文章的翻译版本 public function quickbar() { $locales = config('app.locales'); $buttons = new \stdClass; foreach ($locales as $locale => $language) { $buttons->$locale = n

在Laravel5.1中,我在Article.php模型上创建了上述函数。其想法是,它将检查每种提供的语言是否存在文章的翻译版本

public function quickbar()
{
    $locales = config('app.locales');
    $buttons = new \stdClass;
    foreach ($locales as $locale => $language)
    {
        $buttons->$locale = new \stdClass; 
        if($this->translate($locale))
        {
            $buttons->$locale->class = "exists";
            $buttons->$locale->link = route('articles.edit', ['slug' => $this->slug, 'locale' => $locale]);
        } else {
            $buttons->$locale->class = "missing";
            $buttons->$locale->link = route('articles.create', ['slug' => $this->slug, 'locale' => $locale]);
        }

        return $buttons;
    }
}
返回:

$locales = config('app.locales');
现在,foreach循环应该为这些语言中的每种语言创建一个嵌套的stdClass对象,但我只收到En:

[
 "en" => "English",
 "fr" => "French",
 "nl" => "Dutch",
 "it" => "italian",
 "de" => "German",
]

我不明白为什么我没有收到其他语言

您似乎已将您的
return
放在foreach中,因此循环将在第一个区域设置之后结束。试着把它放在前面。

真丢脸!非常感谢你提出了这么一个愚蠢而简单的问题。有时它需要的只是新鲜的眼睛!
=> {#828
 +"en": {#830
   +"class": "exists",
   +"link": "http://multilingual.dev/articles/loading-efficiently/edit/en",
 },
}