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

时间:2018-09-26 16:05:55

标签: php laravel laravel-5 eloquent laravel-5.5

我与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');
    }
}

3 个答案:

答案 0 :(得分:2)

好的,所以问题出在您的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');
}

答案 1 :(得分:0)

您不能在Y = Y - 1上致电get_query()

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

答案 2 :(得分:0)

您需要在此处通过首先调用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();