gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:32:14 25 4
gpt4 key购买 nike

我在 Laravel 上工作了很多,从来没有遇到过以下错误:

/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php 第 558 行处调用成员函数 getQuery() 为空

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

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

型号证书

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

最佳答案

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

关于php - 在 null Laravel Eloquent 上调用成员函数 getQuery(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52522015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com