laravel-无法将值存储在soome列中 [英] laravel- unable to store values in soome columns

查看:55
本文介绍了laravel-无法将值存储在soome列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册控制器(由Laravel提供),并且我有两种不同的注册表格(客户和经销商),并且它们都使用相同的控制器.两种形式之间的区别在于,某些输入字段采用一种形式,而另一种形式则不是.所以我的代码工作正常,但是我在经销商表格中添加了三个新字段(也增加了三个新列),并且在我注册时没有在occupationdate of birthgenderethnicity列中插入

I have a register controller (provided by Laravel) and I have two different registration forms (Customer and Dealer) and they both use the same controller. The difference between the two forms is that certain input fields are in one form but not the other. So my code works fine but I added three new fields (three new columns as well) to my dealer form and it's not making an insert to occupation, date of birth,gender, and ethnicity columns when I registered it.

我的RegisterController.php:

My RegisterController.php:

 protected function create(array $data)
    {

        $user = User::create([
            // Users table

            'email' => $data['email'],
            'password' => Hash::make($data['password'])
        ]);

        $user->userInfo()->create([
            'name' => $data['name'],
            'NRIC' => $data['nric'], // Create NRIC field.

        ]);

        $user->userAddresses()->create([
            'address_1' => $data['homeaddress1'],
            'address_2' => $data['homeaddress2'],
            'address_3' => $data['homeaddress3'],
            'zipcode' => $data['postcode'],


        ]);

        $user->userContacts()->create([

            'mobile_num' => $data['number'],
            'emergency_num' => $data['emergency']

        ]);

        // check if dealer form is registered, assign dealer role or otherwise

        if ($data['RegistrationForm'] == 2) {
            //assign track id code to dealer
            $user->track_id = 1911000000 + $user->user_id;
            $user->userInfo()->occupation = $data['occupation'];
            $user->userInfo()->ethnicity = $data['race'];
            $user->userInfo()->date_of_birth = $data['dob'];
            $user->userInfo()->gender = $data['gender'];
            $user->save();
            $user->assignRole('1');
            $user->assignRole('2');
        } else {

            //assign track id code to customer
            $user->track_id = 1913000000 + $user->user_id;

            $user->userAddresses()->shipping_address = $data['shippingaddress'];
            $user->save();
            $user->assignRole('1');
        }

        return $user;
    }
}

我检查了我的模型,它们看起来还不错.

I checked my models and they seemed fine.

UserInfo模型:

UserInfo model:

class UserInfo extends Model
{
    // Set table
    protected $table = 'user_infos';

    // Set timestamps
    public $timestamps = true;

    // Set primary key
    protected $primaryKey = 'user_id';

    // Set mass assignable columns
    protected $fillable = [
        'name',
        'NRIC',
        'dealer_id',
        'ethnicity',
        'gender',
        'date_of_birth',
        'occupation'


    ];




    /**
     * Get the user info associated with the user.
     */

    public function user()
    {

        return $this->belongsTo('App\Models\Users\User', 'user_id');
    }
}

track_idassignRole可以很好地插入,但不能插入我添加的那些新列.

track_id and assignRole inserts fine but not those new columns I added.

我在这里犯错了吗?

推荐答案

由于未正确保存Userinfo,因此未保存值.

The values are not getting saved because you are not saving Userinfo properly.

执行

if ($data['RegistrationForm'] == 2) {
        //assign track id code to dealer
        $user->track_id = 1911000000 + $user->user_id;
        $user->save();

        $userinfo = $user->userInfo;

        $userinfo->occupation = $data['occupation'];
        $userinfo->ethnicity = $data['race'];
        $userinfo->date_of_birth = $data['dob'];
        $userinfo->gender = $data['gender'];
        $userinfo->save();

        $user->assignRole('1');
        $user->assignRole('2');
    } else {

        //assign track id code to customer
        $user->track_id = 1913000000 + $user->user_id;

        $user->userAddresses()->shipping_address = $data['shippingaddress'];
        $user->save();
        $user->assignRole('1');
    }

这篇关于laravel-无法将值存储在soome列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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