Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing argument 1 for Illuminate\Support\Collection::get()

Tags:

laravel

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.

like image 914
wobsoriano Avatar asked Sep 03 '15 01:09

wobsoriano


2 Answers

Do not use the all method:

public function boot()
{
    $news = News::take(5)->get();

    view()->share('sideNews', $news);
} 
like image 165
khuong tran Avatar answered Oct 16 '22 13:10

khuong tran


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'));
    }
like image 3
Mayank Dudakiya Avatar answered Oct 16 '22 14:10

Mayank Dudakiya