0

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

Call to undefined method Illuminate\Database\Query\Builder::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 Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

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 Illuminate\Database\Query\Builder::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?

2 Answers 2

1

I think you change

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

to

$usuario->save();

Then just run your polymorphic relationship as normal afterwards.

1
  • You mean doing $usuario->save(); then this $usuario->cuenta()->save($alumno); ?
    – aerojun
    May 12, 2014 at 0:21
0

I did it the wrong way. (?)

Changed

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

To

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.