Netra Netra - 18 days ago 5
PHP Question

Laravel 5 Method [ALL] does not exist

I'm trying to make a CRUD application in Laravel 5 to better understand how a php platform work ( first time using one ), but I keep getting the "Method [all] does not exist." error and I can't understand what i'm doing wrong. I have a table named 'products' in Mysql and what I'm trying to do, is to list all the entry's from within the table. I also have an index.blade.php that I will post if needed.

ProductsController.php

class ProductsController extends Controller {

/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$products = ProductsController::all();

return View::make('products.index')->with('products', $products);
}


Products.php

class Products extends Eloquent
{

}


routes.php

Route::resource('/', 'ProductsController@index');

Answer

Eloquent will assume the User model stores records in the users table. You may specify a custom table by defining a $table property on your model. [Ref]

So, you should rename your Products.php to Product.php (or define the $table property on your model).

Then you can retrieve all products:

$products = Product::all();

Product.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model {

    //

}

ProductsController.php

<?php namespace App\Http\Controllers;

use App\Product;

class ProductsController extends Controller {

    public function index()
    {
        $products = Product::all();

        return View::make('products.index')->with('products', $products);
    }

}