2015-04-11 5 views
1

Я пытаюсь сделать приложение CRUD в Laravel 5, чтобы лучше понять, как работает платформа php (первый раз использует один), но я продолжаю получать «Method [all] не существует." ошибка, и я не могу понять, что я делаю неправильно. У меня есть таблица с именем «products» в Mysql, и я пытаюсь сделать список всех записей из таблицы. У меня также есть index.blade.php, который я отправлю, если потребуется.Laravel 5 Метод [ALL] не существует

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('/', '[email protected]'); 

ответ

5

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 ]

Таким образом, вы должны переименовать Products.php к Product.php (или определить тыс e $table недвижимость на вашей модели).

Тогда вы можете получить все продукты:

$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); 
    } 

} 
+0

Если я делаю это я получаю ошибку FatalErrorE xception в ProductsController.php строка 17: Class 'App \ Http \ Controllers \ Product' not found – Netra

+0

Вы должны импортировать модель: 'use App \ Product;' –

1

Почему вы пишете ProductsController ас cess all(). Для доступа к функции all() необходимо вызвать модель продукта.

ProductsController::all(); 

Пример

Модель продукта

class Product extends Model 
{ 
    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'products'; 
} 

Контроллер продукта

class ProductController extends Controller { 

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

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

Если я сделаю это так, я все равно получаю сообщение об ошибке: FatalErrorException в ProductsController.php строка 17: Class 'App \ Http \ Controllers \ Product' не найден – Netra

+0

Поместите 'приложение App \ Product' в контроллер продукта после пространства имен или вызовите это в Product COntroller 'App \ Product :: all(); ' – Faiz

Смежные вопросы