保存多态关系时调用未定义的Builder :: save() [英] Call to undefined Builder::save() when saving a polymorphic relationship
本文介绍了保存多态关系时调用未定义的Builder :: save()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在注册用户时保存多态关系,但是它返回了我:
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()
我的数据库中有3个表:
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
模型:
<?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. :)
更新
将我保存模型的方式更改为:
Changed the way i save the models to:
$alumno->save();
$usuario->save();
$usuario->cuenta()->save($alumno);
返回 调用未定义的方法Illuminate \ Database \ Query \ Builder :: save()
It returns Call to undefined method Illuminate\Database\Query\Builder::save()
还使用:
$alumno->save();
$usuario->save();
$usuario->cuenta()->associate($alumno);
返回
Maximum function nesting level of '100' reached, aborting!
我应该使用FK吗?
推荐答案
我做错了方法. (?)
I did it the wrong way. (?)
已更改
$usuario->cuenta()->save($alumno);
收件人
$alumno->user()->save($usuario);
这篇关于保存多态关系时调用未定义的Builder :: save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文