调用未定义的函数App \ Http \ Controllers \ Post()

时间:2019-07-07 08:59:25

标签: laravel eloquent laravel-5.8

他试图显示从dbindex page的数据,但显示:Call to undefined function App\Http\Controllers\Post()

控制器:

  <?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use App\Post;

  class IndexController extends Controller
  {

      public function index()
      {
          $data = Post();
          return view('index', compact('data'));
          //return view('index');
      }

我该如何解决

4 个答案:

答案 0 :(得分:2)

您试图将Post()作为方法/函数而不是Model Post来调用,并且由于Post()不存在,因此会出现此错误。

要使用您的帖子模型返回所有​​帖子,您应该这样写:

$data = Post::all(); // To return all posts

或者如果您想基于数据库中的某些字段对其进行过滤

$data = Post::where('active', 1)->get(); // Get all active posts (you need to adjust to any field you have on your database)

您的代码可能类似于:

 <?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use App\Post;

  class IndexController extends Controller
  {

      public function index()
      {
          $data = Post::all();
          return view('index', compact('data'));
      }

请在这里阅读有关雄辩的更多信息: https://laravel.com/docs/5.8/eloquent

答案 1 :(得分:0)

正确的语法应为:

An unhandled exception occurred while processing the request.

docs:https://laravel.com/docs/5.8/requests#accessing-the-request

答案 2 :(得分:0)

您应该修改您的代码:

def ftn1():
   ...
def ftn2():
   ...
x=0;
itertools.product(x=ftn1(x), x=ftn2(x), .., x=ftnk(x), [repeat=10])

获取所有帖子

答案 3 :(得分:0)

$data = Post();

将all()方法添加到此行

$data = Post::all();