Laravel 5.3在null

时间:2016-10-20 09:53:41

标签: methods fatal-error laravel-5.3

我收到以下错误:

  

EloquentVehicle.php第30行中的FatalErrorException:呼叫成员   函数groupBy()on null

我有以下代码:

<?php

namespace App\Project\Frontend\Repo\Vehicle;

use Illuminate\Database\Eloquent\Model;

class EloquentVehicle implements VehicleInterface
{
    protected $vehicle;

    /**
     * EloquentVehicle constructor.
     *
     * @param Model $vehicle
     */
    public function __construct(
        Model $vehicle
    )
    {
        $this->$vehicle = $vehicle;
    }

    /**
     * Fetch unique makes
     *
     * @return mixed
     */
    public function fetchMakes()
    {
        return $this->vehicle->groupBy(array('make'))
            ->orderBy('make', 'asc')
            ->get();
    }
}

我已经检查了Illuminate \ Database \ Eloquent \ Model中显然不存在的方法,但我不知道应该添加到我的类中,以便我可以使用groupBy方法。 laravel文档说该方法存在。

更新:显然我无法输入抽象类。我不知道我应该如何使用Eloquent来检索记录。如果有帮助,下面是我将类注册到服务容器的代码

<?php

namespace App\Providers;

use App\Vehicle;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
use Illuminate\Support\ServiceProvider;

class RepoServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('App\Project\Frontend\Repo\Vehicle\VehicleInterface', function($app)
        {
            return new EloquentVehicle(
                new Vehicle
            );
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我只是发现了自己的错误而且确实把脸埋在手掌里。

$this->$vehicle = $vehicle;

应该是这个

$this->vehicle = $vehicle;