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:
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.
Do not use the all
method:
public function boot()
{
$news = News::take(5)->get();
view()->share('sideNews', $news);
}
I have faced this issue while paginate() function.
Simple Solution
Remove get()
after paginate()
or take()
What cause this error?
If we use get()
function after paginate()
or take()
like paginate(5)->get()
then this error will occur.
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'));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With