laravel 在laravel中保存相关记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23228694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:46:53  来源:igfitidea点击:

Saving related records in laravel

laravellaravel-4one-to-manyeloquent

提问by BeardedInBindary

I have users, and users belong to a dealership.

我有用户,用户属于经销商。

Upon user registration, I'm trying to save a new user, and a new dealership.

用户注册后,我试图保存一个新用户和一个新经销商。

User database has a dealership_idcolumn, which I want to be populated with the ID of the newly created dealership.

用户数据库有一dealership_id列,我想用新创建的经销商的 ID 填充它。

This is my current code in the UserControllerstore method.

这是我在UserControllerstore 方法中的当前代码。

public function store()
{
    $user = new User();
    $user->email = Input::get('email');
    $user->password = Input::get('password');


    $dealership = new Dealership();
    $dealership->name = Input::get('dealership_name');

    $user->push();
    return "User Saved";

}

Trying to use $user->push();User data gets updated, but dealership is not created or updated.

尝试使用$user->push();用户数据会更新,但不会创建或更新经销商。

回答by Quasdunk

Eloquent's push()saves the model and its relationships, but first you have to tell what you want to be involved in the relationsship.

Eloquentpush()保存了模型和它的关系,但首先你必须说明你想在关系中参与什么。

Since your user-model/table holds the id of the dealership, I assume that a user can belong to only one dealership, so the relationship should look like this:

由于您的用户模型/表包含经销商的 id,我假设一个用户只能属于一个经销商,因此关系应如下所示:

User Model:

用户模型:

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

Dealership Model:

经销商模式:

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

To save a User from the Dealership perspective, you do this:

要从经销商的角度保存用户,请执行以下操作:

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

To associate a dealership with a user, you do this:

要将经销商与用户关联,请执行以下操作:

$user->dealership()->associate($dealership);
$user->save();

回答by Paul Bele

Please check this answerto see the difference of push() and save()

请检查此答案以了解 push() 和 save() 的区别

You will need to define correctly your models relationships as per documentationIf this is done correctly, it should work . This is what push() does :

您将需要根据文档正确定义模型关系如果正确完成,它应该可以工作。这就是 push() 的作用:

/**
 * Save the model and all of its relationships.
 *
 * @return bool
 */

    public function push()
    {
        if ( ! $this->save()) return false;

        // To sync all of the relationships to the database, we will simply spin through
        // the relationships and save each model via this "push" method, which allows
        // us to recurse into all of these nested relations for the model instance.

        foreach ($this->relations as $models)
        {
            foreach (Collection::make($models) as $model)
            {
                if ( ! $model->push()) return false;
            }
        }

        return true;
    }

In your case, you have a one (dealership) belongs to many (users)

在您的情况下,您有一个(经销商)属于许多(用户)

In your Users model :

在您的用户模型中:

class Users extends Eloquent {

    public function dealership()
    {
        return $this->belongsTo('Dealership');

    }

}

In the example above, Eloquent will look for a dealership_id column on the users table. In your Dealership Model :

在上面的示例中,Eloquent 将在 users 表中查找 Dealship_id 列。在您的经销商模式中:

class Dealership extends Eloquent {

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

}

In your store function :

在您的商店功能中:

 public function store()
    {
        $user = new User();
        $user->email = Input::get('email');
        $user->password = Input::get('password');


        $user->dealership = new Dealership();
        $user->dealership->name = Input::get('dealership_name');

        $user->push();
        return "User Saved";

    }  

Learn here more about eloquent relationships

在此处了解有关雄辩关系的更多信息

Also please take a look at my answer here

也请看看我的回答here

回答by seeARMS

By using push on the User model, Laravel is basically recursively calling save on all the related models (which, in this case, is none, since you haven't associated any other models to it yet).

通过在 User 模型上使用 push ,Laravel 基本上是递归调用所有相关模型的 save (在这种情况下,没有,因为您还没有将任何其他模型关联到它)。

Therefore, in order to accomplish what you're trying to do, you can do first create the user then associate the dealership with it by doing the following:

因此,为了完成您想要做的事情,您可以先创建用户,然后通过执行以下操作将经销商与其关联:

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

$dealership = new Dealership();
$dealership->name = Input::get('dealership_name');

$user->dealerships()->save($dealership);
return "User Saved";

However, prior to doing this, you must ensure your User and Dealership models have their relationships set up correctly:

但是,在执行此操作之前,您必须确保您的用户和经销商模型已正确设置它们的关系:

User Model:

用户模型:

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

Dealership Model:

经销商模式:

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

回答by Thayanandan Samiappan Kandiar

This is how I manage to do it.

这就是我设法做到的。

In your controller:(Laravel 5)

在您的控制器中:(Laravel 5)

use Auth;

$dealership->user = Auth::user()->ref_user->staff_id;