doucaigai7176 2016-01-04 17:08
浏览 59
已采纳

在非对象上调用成员函数getPaginate()

I'm new to laravel framwork , and I'm coding my first web app

and getting the following error

 FatalErrorException in PersonController.php line 26:
Call to a member function getPaginate() on a non-object

this is my Controller

   <?php

namespace App\Http\Controllers; 
use App\Repositories\PersonRepository;

class PersonController extends Controller
{
    protected  $personRepo ;
    protected  $nbrPerPage = 4 ;


    public  function  _construct(PersonRepository $personRepository)
    {
        $this->personRepo = $personRepository ;
    }
    public function index()
    {
        $persons = $this->personRepo->getPaginate(nbrPerPage);
        $links = $persons->setPath('')->render();

        return view('index', compact('persons', 'links'));
    }

    public function create()
    {

    }


    public function store()
    {

    }


    public function show($id)
    {
        //
    }


    public function edit($id)
    {
        //
    }


    public function update($id)
    {
        //
    }


    public function destroy($id)
    {
        //
    }


}

and this my repository class

<?php
namespace  App\Repositories ;
use App\Person ;
use App\User;


class PersonRepository {

  protected  $person ;
    public function  _construct (Person $person)
    {
        $this->$person = $person  ;
    }


    public  function  getPaginate($n)
    {

        return $this->person-> paginate($n) ;
    }


 }
  • 写回答

2条回答 默认 最新

  • donglu6805 2016-01-04 21:35
    关注

    Unless these are just typos in the question, you have a lot of typos in your code.

    The typo that is causing this specific error is that the name of the constructor method should be __construct (with two underscores), not _construct (with one underscore).

    Since the constructor method is misspelled on your PersonController, this method is never called and the personRepo attribute is never set. Since it is never set, the line $persons = $this->personRepo->getPaginate(nbrPerPage); is trying to call getPaginate() on a non-object.

    Additional typos/issues I see at a glance:

    • $persons = $this->personRepo->getPaginate(nbrPerPage);
      nbrPerPage is being used as a constant. This is incorrect. Should be:
      $persons = $this->personRepo->getPaginate($this->nbrPerPage);
    • Constructor on PersonRepository also misspelled. Should be __construct(), not _construct.
    • $this->$person = $person ;
      This is inside the attempted construct of the PersonRepository. The $ needs to be removed from $this->$person. Should be:
      $this->person = $person;
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀