在 null Laravel Eloquent 上调用成员函数 getQuery()

Call to a member function getQuery() on null Laravel Eloquent

我使用 Laravel 进行了大量工作,但从未遇到过以下错误:

Call to a member function getQuery() on null/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php line 558

我正在尝试获取包含所有指定关系的记录。导致该错误的代码如下:

        $id = 7;
        $compra = Compra::where('id', $id)
                            ->with(['certificado',
                                    'certificado.duracionServicios', 
                                    'certificado.duracionServicios.servicio', 
                                    'certificado.duracionServicios.servicio.traducciones' => function($query){
                                         $query->whereHas('idiomas', function($q){
                                             $q->where('codigo_region', 'es_MX');
                                         });
                                     },
                                    'user'])
                                    ->first();

如果我取出first()方法,我可以打印Builder的对象实例,但是当我尝试使用它甚至使用get()时,抛出异常. Laravel 我使用的版本是 5.5。

型号Compra

namespace App\Models\Sitio;

use Illuminate\Database\Eloquent\Model;

class Compra extends Model
{
    protected $table = 'compras';

    protected $fillable = ['folio',
                            'fecha_compra',
                            'subtotal',
                            'total',
                            'user_id',
                            'direccion_id',
                            ];


    protected $dates = ['created_at', 'updated_at'];

    /**
     * Get the value of the model's route key.
     *
     * @return mixed
     */
    public function getRouteKey()
    {
        $hashids = new \Hashids\Hashids(config('app.name'), 5);

        return $hashids->encode($this->getKey());
    }


    public function certificado(){
        $this->hasOne('App\Models\Sitio\Certificado', 'compra_id');
    }

    public function user(){
        return $this->belongsTo('App\User', 'user_id');
    }                   
}

型号Certificado

namespace App\Models\Sitio;

use Illuminate\Database\Eloquent\Model;

class Certificado extends Model
{
    protected $table = 'certificados';

    protected $fillable = ['fecha_servicio',
                            'hora_servicio',
                            'compra_id',
                            ];


    protected $dates = ['created_at', 'updated_at'];

    public function compra(){
        return $this->belongsTo('App\Models\Sitio\Compra', 'compra_id');
    }

    public function duracionServicios(){
        return $this->belongsToMany('App\Models\Sitio\DuracionServicio', 'certificados_duracion_servicios', 'certificado_id', 'duracion_id');
    }
}

型号DuracionServicio

namespace App\Models\Sitio;

use Illuminate\Database\Eloquent\Model;

class DuracionServicio extends Model

{
    protected $table = 'duracion_servicios';

protected $fillable = ['costo',
                        'servicio_id',
                        ];

protected $dates = ['created_at', 'updated_at'];

public function certificados(){
    return $this->belongsToMany('App\Http\Models\Sitio\Certificado', 'certificados_duracion_servicios', 'duracion_id', 'certificado_id');
}

public function servicio(){
    return $this->belongsTo('App\Models\Sitio\Servicio', 'servicio_id');
}

public function traducciones(){
    return $this->hasMany('App\Models\Sitio\DuracionServicioTraduccion', 'duracion_id');
}
}

型号Servicio

namespace App\Models\Sitio;

use Illuminate\Database\Eloquent\Model;

class Servicio extends Model
{
    protected $table = 'servicios';

    protected $fillable = ['seccion_id'];

    protected $dates = ['created_at', 'updated_at'];


    public function traducciones(){
        return $this->hasMany('App\Models\Sitio\ServicioTraduccion', 'servicio_id');
    }

    public function duraciones(){
        return $this->hasMany('App\Models\Sitio\DuracionServicio', 'servicio_id');
    }
}

型号ServicioTraduccion

namespace App\Models\Sitio;

use Illuminate\Database\Eloquent\Model;

class ServicioTraduccion extends Model
{
    protected $table = 'servicios_traducciones';

    protected $fillable = ['nombre',
                            'descripcion', 
                            'contenido', 
                            'idioma_id', 
                            'servicio_id',
                            ];

    protected $dates = ['created_at', 'updated_at'];

    public function servicio(){
        return $this->belongsTo('App\Models\Sitio\Servicio', 'servicio_id');
    }

    public function idioma(){
        return $this->belongsTo('App\Models\Sitio\Idioma', 'idioma_id');
    }
}

型号Idioma

namespace App\Models\Sitio;

use Illuminate\Database\Eloquent\Model;

class Idioma extends Model
{
    protected $table = 'servicios_traducciones';

    protected $fillable = ['nombre',
                            'codigo_region',
                            ];

    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

    public function servicios(){
        return $this->hasMany('App\Models\Sitio\ServicioTraduccion', 'idioma_id');
    }

    public function duraciones(){
        return $this->hasMany('App\Models\Sitio\DuracionServicioTraduccion', 'idioma_id');
    }
}

您不能在 ->first() 上调用 get_query()

->first() 将 return 您查询的第一个结果,因此执行 SQL 查询(在 first() 之后您会得到 null 结果如果没有结果,或者你的查询结果对象)。如果您删除 first(),您应该能够使用 get_query()get(),因为您将拥有一个 eloquent 查询实例,而不是结果对象或集合。

好的,所以问题出在您的 Compra 模型中。

您这样定义关系:

public function certificado()
{
    $this->hasOne('App\Models\Sitio\Certificado', 'compra_id');
}

您忘记添加 return。应该是:

public function certificado()
{
    return $this->hasOne('App\Models\Sitio\Certificado', 'compra_id');
}

您需要先在此处调用 query() 方法来建立查询。

       $id = 7;
       $compra = Compra::query()->where('id', $id)
        ->with(['certificado',
                'certificado.duracionServicios', 
                'certificado.duracionServicios.servicio', 
                'certificado.duracionServicios.servicio.traducciones' => function($query){
                        $query->whereHas('idiomas', function($q){
                            $q->where('codigo_region', 'es_MX');
                        });
                    },
            'user'])
            ->get();

或者您可以使用 ->first();