我试图在 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 theusers
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/