保存多态关系时调用未定义的Builder :: save()

编程入门 行业动态 更新时间:2024-10-13 16:19:13
本文介绍了保存多态关系时调用未定义的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()

本文发布于:2023-08-04 09:16:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1295030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   关系   未定义   多态   save

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!