简体   繁体   中英

laravel mass assignment setting not working

When I insert user information into my app I use the following code:

$userId = Auth::user()->user_id;

$newUserInfo = UserInfo::create([
            'u_info_id' => $userId,
            'u_first_name' => Input::get('u_first_name'),
            'u_last_name' => Input::get('u_last_name'),
            'u_phone_number' => Input::get('u_phone_number'),
            'u_address' => Input::get('u_address'),
            'u_state_id' => Input::get('u_state_id'),
            'u_city_id' => Input::get('u_city_id'),
            'u_zip' => Input::get('u_zip')
        ]);

But I cant do this 'u_info_id' => $userId since that will force me to make u_info_id fillable for the mass assignment.

So my problem is, how can I use 'u_info_id' => $userId without making u_info_id fillable for mass assignment. I dont want users to be able to edit u_info_id . That field should be inserted backend.

OR, do I only need to set fillable mass assignment when using laravel ->fill() ?

I solved it like this:

//Validation of inputs

$newUserInfo = new UserInfo();

        $newUserInfo->u_info_id = $userId;
        $newUserInfo->u_first_name = Input::get('u_first_name');
        $newUserInfo->u_last_name = Input::get('u_last_name');
        $newUserInfo->u_phone_number = Input::get('u_phone_number');
        $newUserInfo->u_address = Input::get('u_address');
        $newUserInfo->u_state_id = Input::get('u_state_id');
        $newUserInfo->u_city_id = Input::get('u_city_id');
        $newUserInfo->u_zip = Input::get('u_zip');

        $newUserInfo->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM