doucaigai7176 2016-01-04 09: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 13: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部