samedi 6 mai 2017

Laravel 5 Call to undefined method Illuminate\Database\Query\Builder::muestras()

I'm working with Laravel 5.1: I have a model called Muestra that has 4 foreign keys/relationships one to many with Ganado, TipoMuestra, TipoConsulta and Laboratorio. The problem comes when I create a Muestra element called $muestra in guardarNueva() from a form, and it calls $muestra->setTipoConsulta/Laboratorio/TipoMuestra(), it calls to the function with and throws the error:

BadMethodCallException in Builder.php line 2123:
Call to undefined method 
Illuminate\Database\Query\Builder::muestras()
in Builder.php line 2123
at Builder->__call('muestras', array())
at call_user_func_array(array(object(Builder), 'muestras'), array()) 
in Builder.php line 1015
at Builder->__call('muestras', array())
at call_user_func_array(array(object(Builder), 'muestras'), array()) 
in Model.php line 3444
at Model->__call('muestras', array()) in Muestra.php line 51
at Muestra->setTipoConsulta(object(TipoConsulta)) in Muestra.php line 65
at Muestra::guardarNueva(object(Request)) in MuestrasController.php line 48

Muestra model code is:

class Muestra extends Model
{
protected $fillable = ['tubo'];
protected $dates = ['fecha_extraccion'];

public function ganado()
{
    return $this->belongsTo(Ganado::class);
}

public function tipo_muestra()
{
    return $this->belongsTo(TipoMuestra::class);
}

public function tipo_consulta()
{
    return $this->belongsTo(TipoConsulta::class);
}

public function laboratorio()
{
    return $this->belongsTo(Laboratorio::class);
}

public function setFechaExtraccion($date)
{
    $this->fecha_extraccion = $date;
    $this->save();
    return $this->fecha_extraccion;
}

public function setLaboratorio($laboratorio){
    return $laboratorio->muestras()->save($this);

}

public function setTipoMuestra($tipomuestra){
    return $tipomuestra->muestras()->save($this);

}

public function setTipoConsulta($tipoconsulta){
    return $tipoconsulta->muestras()->save($this);

}
public function setGanado($ganado){
    return $ganado->muestra()->save($this);

}

public static function guardarNueva($request){
    $datos = $request->except(['tipo_muestra_id','tipo_consulta_id','laboratorio_id','ganado_id','fecha_extraccion']);
    $muestra=self::create($datos);
    $muestra->setFechaExtraccion($request->input('fecha_extraccion'));
    $muestra->setGanado(Ganado::find($request->input('ganado_id')));
    $muestra->setLaboratorio(Laboratorio::find($request->input('laboratorio_id')));
    $muestra->setTipoConsulta(TipoConsulta::find($request->input('tipo_consulta_id')));
    $muestra->setTipoMuestra(TipoMuestra::find($request->input('tipo_muestra_id')));
    return $muestra;

}

}

The migration of Muestra is:

class CreateMuestrasTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('muestras', function (Blueprint $table) {
        $table->increments('id');
        $table->string('tubo')->unique();
        $table->date('fecha_extraccion');
        $table->integer('ganado_id');
        $table->integer('tipo_muestra_id');
        $table->integer('tipo_consulta_id');
        $table->integer('laboratorio_id');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('muestras');
}
}

And any of the other models that are failing, for example TipoMuestra is like this:

class TipoConsulta extends Model
{
//
protected $fillable=['nombre'];

protected function muestras(){
    return $this->hasMany(Muestra::class);
}


public function getSelectOptionAttribute(){
    return $this->attributes['nombre'];
}
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire