Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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 如何在laravel eloquent返回的数组中使用next()或current()?_Php_Laravel_Eloquent - Fatal编程技术网

Php 如何在laravel eloquent返回的数组中使用next()或current()?

Php 如何在laravel eloquent返回的数组中使用next()或current()?,php,laravel,eloquent,Php,Laravel,Eloquent,我正在建立预订系统,我正在编程递归功能。因此,我需要在php中使用php函数next()和current()。问题是,如果我有雄辩的所有记录的数组,它就是不起作用。它可以在其他数组中工作,所以我猜内部指针没有设置,但是reset()函数也没有帮助 问题是,当我调用laravel中的Elount模型以从如下表中获得所有结果时: $posts = Post::all(); 然后我回显或返回(不重要)当前($posts),我得到的结果与返回$posts(整个数组)的结果相同 当我试着称之为: nex

我正在建立预订系统,我正在编程递归功能。因此,我需要在php中使用php函数next()和current()。问题是,如果我有雄辩的所有记录的数组,它就是不起作用。它可以在其他数组中工作,所以我猜内部指针没有设置,但是reset()函数也没有帮助

问题是,当我调用laravel中的Elount模型以从如下表中获得所有结果时:

$posts = Post::all();
然后我回显或返回(不重要)当前($posts),我得到的结果与返回$posts(整个数组)的结果相同

当我试着称之为:

next($posts);
return current($posts);
或:

我有一个错误:

The Response content must be a string or object implementing __toString(), "boolean" given.
有谁能帮我解决这个问题吗?如果我制作了相同的数组并返回它,那么eloquent数组和这个新创建的数组是相同的,但是如果我在eloquent数组上使用current,它就不起作用了。
谢谢您的帮助。

要检索第一行:
Post::first()

要将集合转换为数组:
Post::all()->toArray()


要检索第一行:
Post::first()

要将集合转换为数组:
Post::all()->toArray()

你想要

$posts = Post::all()->toArray();
然后可以调用
current($posts)
next($posts)
prev($posts)
end($posts)

但是,当您拥有一个集合时,您可以始终使用laravel的集合方法,例如
first
last
each
filter
map
reduce

你想要

$posts = Post::all()->toArray();
然后可以调用
current($posts)
next($posts)
prev($posts)
end($posts)

但是,当您拥有一个集合时,您可以始终使用laravel的集合方法,例如
first
last
each
filter
map
reduce

我使用以下代码:

$models = User::whereIn('name',$names)->get()->getIterator();

while ( $record = $models->current() )
{
    ...
    $models->next();
}
我使用以下代码:

$models = User::whereIn('name',$names)->get()->getIterator();

while ( $record = $models->current() )
{
    ...
    $models->next();
}

Post::all()
返回集合,而不是数组。您可以
toArray()
它,但我怀疑有更好的方法来处理这个问题。
Post::all()
返回一个集合,而不是数组。你可以
toArray()
it,但我怀疑会有更好的方法来处理这个问题。非常感谢,我想这是一件非常基本的事情,所以我想我应该先学这个,但非常感谢。非常感谢,我想这是一件非常基本的事情,所以我想我应该先学这个,但是非常感谢你。
$models = User::whereIn('name',$names)->get()->getIterator();

while ( $record = $models->current() )
{
    ...
    $models->next();
}