laravel插入多个表 [英] laravel inserting into multiple tables

查看:356
本文介绍了laravel插入多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子

#something - id, name, url
#something_users - id, id_something, email, password

我的模型

class Something extends Eloquent
{

    protected $table = 'something';

    protected $fillable = ['name', 'email', 'password'];

    public $errors;


    public function User()
    {
        return $this->belongsTo('User', 'id', 'id_something');
    }
}

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;


    protected $table = 'something_users';

    protected $hidden = array('password', 'remember_token');

    public function Something()
    {
        return $this->belongsTo('Something');
    }

}

控制器

$input = Input::all();

// also some validation
$this->db->fill($input);

$this->db->password = Hash::make(Input::get('password'));

$this->db->push();

$this->db->save();

SQL

insert into `something` (`name`, `email`, `password`) values...

我需要在第一个表(东西)中插入名称,并在第二个表(something_users)中输入电子邮件和密码

I need to insert name into the first table(something) and email, password into second(something_users)

该怎么做?我对此有线索.

How to do that? I have on clue about that.

推荐答案

您的关系有些混乱,您可能想更改这些关系.请注意hasMany()belongsTo().如果something只能有一个user,则可能希望将函数从hasMany()更改为hasOne(),并将函数名更改为user(),因为这样更有意义.

Your relationships are a little screwed up, you probably want to change those. Note the hasMany() vs the belongsTo(). If a something can only have one user, you may wish to change the function to hasOne() from hasMany() and the name of the function to user() only because it makes more sense that way.

class Something extends Eloquent {

    protected $table = 'something';

    public function users()
    {
        return $this->hasMany('User', 'id_something');
    }
}

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $table = 'something_users';

    protected $hidden = array('password', 'remember_token');

    public function something()
    {
        return $this->belongsTo('Something', 'id_something');
    }
}

然后保存somethinguser并将它们链接起来,这很容易.

And then to save a something and a user and have them linked, it's pretty easy.

$something = new Something;
$something->name = Input::get('name');
$something->url = Input::get('url');
$something->save();

$user = new User;
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));

$something->users()->save($user);

我没有看到您的构造函数,所以我不知道$this->db指的是哪个模型,但是您可能要根据自己的情况替换somethingsusers.为了使依赖注入持续进行,建议您将依赖命名为它们的真实名称.

I'm not seeing your constructor so I don't know which model $this->db refers to, but you may want to replace the somethings or users depending on what you have. To keep your dependency injection going, I'd suggest naming your dependencies what they actually are.

class SomeController extends BaseController {

    public function __construct(User $user, Something $something)
    {
        $this->user = $user;
        $this->something = $something;
    }

    public function someFunction()
    {
        $this->something->name = Input::get('name');
        $this->something->url = Input::get('url');
        $this->something->save();

        $this->user->email = Input::get('email');
        $this->user->password = Hash::make(Input::get('password'));
        $this->something->users()->save($this->user);
    }
}

这篇关于laravel插入多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆