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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测