AntonioNicasio's avatar

Problem trying to store new object from related model on form submit

Hi, guys I have this error y triying to fixing with no luck :( this is the error

BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::encuesta()

use App\Encuesta; use App\Image; use App\Integrante;

this is the relation on encuesta model

public function integrantes()
{
  return $this->hasMany(Integrante::class, 'encuesta_id');
}

this is my model Integrante

namespace App;

use Illuminate\Database\Eloquent\Model;

class Integrante extends Model { protected $fillable = ['nombres', 'curp', 'parentesco', 'fecha_nacimiento', 'genero', 'estado_nacimiento', 'encuesta_id'];

protected $table = 'integrantes';


public function integrantes()
{
    return $this->belongsTo(Encuesta::class);
}

}

this is my method to store

public function store(EncuestaRequest $request){

    // Image Upload
    $file = $request->file('image');
    $file_count = count($file);
    $fileupload = 0; 
    $name = 'imagen_' . time() . '.' . $file->getClientOriginalExtension();
    $path = public_path() . '/uploads';
    $file->move($path, $name);

    $encuestas = new Encuesta($request->all());
    $encuestas->save();
    // Saving Integrante
    $integrantes = new Integrante();
    $integrantes->nombres = $request->nombres;
    $integrantes->curp = $request->curp;
    $integrantes->parentesco = $request->parentesco;
    $integrantes->fecha_nacimiento = $request->fecha_nacimiento;
    $integrantes->genero = $request->genero;
    $integrantes->estado_nacimiento = $request->estado_nacimiento;
    $integrantes->encuesta()->associate($encuestas);
    $integrantes->save();

    // Saving Image   
    $image = new Image();
    $image->name = $name;
    $image->encuesta()->associate($encuestas);
    $image->save();

    
    return redirect('encuesta');
}
0 likes
1 reply
AntonioNicasio's avatar
AntonioNicasio
OP
Best Answer
Level 4

I fix the problem

Just changing the method name on encuestas model

Please or to participate in this conversation.