php - Laravel 5 方法 [ALL] 不存在

标签 php mysql laravel-5

我试图在 Laravel 5 中创建一个 CRUD 应用程序以更好地理解 php 平台的工作原理(第一次使用),但我一直收到“方法 [all] 不存在”。错误,我不明白我做错了什么。我在 Mysql 中有一个名为“products”的表,我想做的是列出表中的所有条目。我还有一个 index.blade.php,如果需要我会发布。

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

产品.php

class Products extends Eloquent
{

}

路由.php

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

最佳答案

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(或在您的模型上定义 $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);
    }

}

关于php - Laravel 5 方法 [ALL] 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29579608/

相关文章:

php - Yii 附加事件

mysql - 如何查询数据库中不相似但几乎匹配的项目

php - 我如何通过从数据库检索代码来输出这种格式?

mysql - 使用复杂的 AND OR 将 SQL 转换为 Laravel 查询生成器

javascript - 有没有办法在 HTML 中将 PHP 对象作为 JavaScript 参数传递?

PHP - 解析新行日志

php - Google 报告了一些实际上并不存在的 404 错误。这是否表明我的网站被黑了?

php - 存储和处理 MySQL 结果

php - 多个sql查询的单个查询

mysql - 嵌套查询中的问题 Mysql问题