laravel Illuminate\Support\Collection::get() 缺少参数 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32365590/
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
Missing argument 1 for Illuminate\Support\Collection::get()
提问by wobsoriano
I have a simple Laravel 5.1 code and I'm getting the ErrorException
Missing argument 1 for Illuminate\Support\Collection::get()
. Here is the code:
我有一个简单的 Laravel 5.1 代码,我得到了ErrorException
Missing argument 1 for Illuminate\Support\Collection::get()
. 这是代码:
public function boot()
{
$news = News::all()->take(5)->get();
view()->share('sideNews', $news);
}
Whenever I remove the ->get();
there, it works. It's my first time using eloquent
. I remember when I'm using the query builder, I always add the ->get()
in the last line of the code. Am i doing it correctly? Thank you.
每当我删除->get();
那里时,它就会起作用。这是我第一次使用eloquent
. 我记得当我使用查询构建器时,我总是->get()
在代码的最后一行添加。我做得对吗?谢谢你。
回答by khuong tran
Do not use the all
method:
不要使用以下all
方法:
public function boot()
{
$news = News::take(5)->get();
view()->share('sideNews', $news);
}
回答by Mayank Dudakiya
I have faced this issue while paginate()function.
我在paginate()函数时遇到了这个问题。
Simple Solution
简单的解决方案
Remove get()
after paginate()
or take()
get()
在paginate()
或之后删除take()
What cause this error?
什么原因导致这个错误?
If we use get()
function after paginate()
or take()
like paginate(5)->get()
then this error will occur.
如果我们使用get()
afterpaginate()
或take()
like函数,paginate(5)->get()
则会发生此错误。
Right way or Answer
正确的方法或答案
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::paginate(5);
return view('product.index',compact('products'));
}