Call to undefined Builder::save() when saving a polymorphic relationship

I’m trying to save a polymorphic relationship when registering a user, but it returns me:

Call to undefined method IlluminateDatabaseQueryBuilder::save()

I have 3 tables on my database:
Schema::create('usuarios', function(Blueprint $table) {
    $table->increments('id');
    $table->string('nombreUsuario', 20);
    $table->string('password', 60);
    $table->string('email', 30);
    $table->string('remember_token', 100)->nullable();
    $table->integer('cuenta_id');
    $table->string('cuenta_type');

    $table->timestamps();
});

Schema::create('empresas', function(Blueprint $table) {
    $table->increments('id');
    $table->string('nombreEmpresa', 50);
    $table->string('direccion', 50);

    $table->timestamps();
});

Schema::create('alumnos', function(Blueprint $table) {
    $table->increments('id');
    $table->string('nombre', 50);
    $table->string('apellidoPaterno', 50);
    $table->string('apellidoMaterno', 50);
    $table->integer('semestre');

       $table->timestamps();
});

On my controller, when a user is being registered:
$alumno = new Alumno;

$alumno->nombre             = Input::get('nombre');
$alumno->apellidoPaterno    = Input::get('paterno');
$alumno->apellidoMaterno    = Input::get('materno');
$alumno->semestre           = Input::get('semestre');

$alumno->save();

$usuario = new User;

$usuario->nombreUsuario     = Input::get('usuario');
$usuario->password      = Hash::make(Input::get('password'));
$usuario->email             = Input::get('email');

$usuario->cuenta()->save($alumno);  // <--Here

The models:
<?php

use IlluminateAuthUserInterface;
use IlluminateAuthRemindersRemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
 ...
    public function cuenta() {
        return $this->morphTo();
    }
}

<?php

class Alumno extends Eloquent {
    protected $fillable = [];

    public function user() {
        return $this->morphMany('User', 'cuenta');
   }    
}

<?php

class Empresa extends Eloquent {
    protected $fillable = [];

    public function user() {
        return $this->morphMany('User', 'cuenta');
    }
}

Every time i try to register someone, it returns this error.

If would be great if someone can show me what i’m doing wrong. Thanks. 🙂

Update

Changed the way i save the models to:

$alumno->save();
$usuario->save();
$usuario->cuenta()->save($alumno);

It returns
Call to undefined method IlluminateDatabaseQueryBuilder::save()

Also used:

$alumno->save();
$usuario->save();
$usuario->cuenta()->associate($alumno);

It returns
Maximum function nesting level of '100' reached, aborting!

Should i use FK?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think you change

$usuario->cuenta()->save($alumno);

to
$usuario->save();

Then just run your polymorphic relationship as normal afterwards.

Method 2

I did it the wrong way. (?)

Changed

$usuario->cuenta()->save($alumno);

To
$alumno->user()->save($usuario);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x