Laravel:Eloquent all() 不返回对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21810085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:02:54  来源:igfitidea点击:

Laravel: Eloquent all() not returning an array of objects

phpmysqllaravel

提问by Karl

I'm trying to build a simple forum system.

我正在尝试建立一个简单的论坛系统。

I have a model called Category.php, where I define the table and extend Eloquent.

我有一个名为 Category.php 的模型,我在其中定义了表并扩展了 Eloquent。

I have a CategoryController.php which is a resourceful controller, within the index() method, I am doing:

我有一个 CategoryController.php,它是一个资源丰富的控制器,在 index() 方法中,我正在做:

$categories = Category::all();

Which I assume should return an array of objects. This worked fine on a different project of mine.

我认为应该返回一个对象数组。这在我的另一个项目中运行良好。

However, when I try to run this through a foreach, nothing is echoed. I've tried renaming the controller and the model to ForumController and Form, respectively, and it's worked straight away.

但是,当我尝试通过 foreach 运行它时,没有任何回应。我已经尝试将控制器和模型分别重命名为 ForumController 和 Form,它立即生效。

Any ideas? Perhaps the Category name has been reserved by Laravel and is overriding my desired functionality?

有任何想法吗?也许类别名称已被 Laravel 保留并且覆盖了我想要的功能?

CategoryController.php:

类别控制器.php:

<?php

class CategoryController extends \BaseController {

protected $layout = 'master';

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    //
    $categories = Category::all();
    $this->layout->content = View::make('category.index', array('categories' => $categories));
}

Category.php (model):

Category.php(模型):

<?php

class Category extends Eloquent {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'categories';

}

Category/index.blade.php (view):

类别/index.blade.php(查看):

@section('content')
<ul>
@foreach ($categories as $category)
    <li><a href="/category/{{ $category->slug }}">{{ $category->name }}</a></li>
@endforeach
</ul>
@stop

My database is set up correctly. When I run $category = Category::find(1); it correctly returns the category with an id of 1, which I can use as an object.

我的数据库设置正确。当我运行 $category = Category::find(1); 它正确返回 id 为 1 的类别,我可以将其用作对象。

回答by SamV

You can iterate over Category::all();. Laravels Collectionclass implements the following interfaces:

您可以迭代Category::all();. LaravelsCollection类实现了以下接口:

ArrayAccess, ArrayableInterface, Countable, IteratorAggregate, JsonableInterface

Which means:

意思是:

foreach(Category::all() as $category) { 
    var_dump($category);
}

Will work, the Eloquent\Collectionthat is returned extends the Support\Collectionclass, which implements the above interfaces.

将工作,Eloquent\Collection返回的扩展了Support\Collection实现上述接口的类。

Take a look at the Support\Collectionclass in Illuminate.

看一看中的Support\CollectionIlluminate

回答by Mark Baker

You're right, when returning multiple results, Eloquent returns a Collection(which is quite common practise among frameworks).

你是对的,当返回多个结果时,Eloquent 返回一个Collection(这在框架中是很常见的做法)。

You can convert a Collectionto other formats using methods like toArray()or toJson(); but you can also iterate through the count()of results using methods like fetch(), or fetch specific entries using get()

您可以Collection使用toArray()或等方法将 a 转换为其他格式toJson();但您也可以count()使用诸如 之类的方法遍历结果fetch(),或使用get()

EDIT

编辑

While you can't iterate directly over the Collection, you can convert it to an iterable structure using the getIterator()method, which returns an SPL ArrayIteratorobject

虽然您不能直接迭代Collection,但您可以使用getIterator()方法将其转换为可迭代结构,该方法返回一个 SPL ArrayIterator对象