繁体   English   中英

在laravel 5.4中的null构造上调用成员函数getAll($ request)

[英]Call to a member function getAll($request) on null construct in laravel 5.4

我用constructget方法创建了一个控制器类:

use App\Repositories\Backend\myObj\myObjContract;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class MyController extends Controller {

  protected $myObj;

  public function construct(myObjContract $myObj) {
    $this->myObj = $myObj;
  }
  public function get(Request $request) {
    $this->myObj->getAll($request);
  }
}

getAll()已在合同中声明,并且也在雄辩的文件中定义。

但我得到一个错误:

在null上调用成员函数getAll($ request)

谁能帮助解决上述错误?

我不确定您要做什么,但是如果您未创建“ getAll”,则可能需要$ request-> all();。

在PHP中,构造函数方法名为__construct (带有两个下划线)。 PHP本质上将您的construct方法解释为常规类方法,因此,当您调用getAll() ,它在一个空变量上。

use App\Repositories\Backend\myObj\myObjContract;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class MyController extends Controller
{

    protected $myObj;

    public function __construct(myObjContract $myObj)
    {
        $this->myObj = $myObj;
    }

    public function get(Request $request)
    {
        $this->myObj->getAll($request);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM