Laravel没有获取我所有的输入值

时间:2018-11-23 11:37:31

标签: php mysql laravel

我是laravel的新手,我一直在尝试获取表单输入值并提交到数据库,但是我一直遇到错误。 如果您可以帮助我浏览一下并帮助检查我缺少输入值的地方,请感到满意。

  

[2018-11-23 11:20:26]开发。错误:SQLSTATE [23000]:完整性   约束违反:1048列“ local_govt_id”不能为空(SQL:   插入users_signedupsstate_idlocal_govt_id,   first_namesurnamephone_numberdate_of_birthtitle,   education_level_iddisciplinework_addressres_address,   occupationgendermarital_statusmake_donations,   be_a_memberbe_a_volunteerabout_youemailpassword,   updated_atcreated_at)值(1,,,,,,,,,,,,,,,,,,,   ,lawalmariamyetunde@gmail.com,,2018-11-23 11:20:23,2018-11-23   11:20:23)){“ exception”:“ [对象]   (Illuminate \ Database \ QueryException(代码:23000):SQLSTATE [23000]:   违反完整性约束:1048列“ local_govt_id”不能为   null(SQL:插入users_signedupsstate_idlocal_govt_id,   first_namesurnamephone_numberdate_of_birthtitle,   education_level_iddisciplinework_addressres_address,   occupationgendermarital_statusmake_donations,   be_a_memberbe_a_volunteerabout_youemailpassword,   updated_atcreated_at)值(1,,,,,,,,,,,,,,,,,,,   ,app @ gmail.com,``2018-11-23 11:20:23,2018-11-23 11:20:23))   C:\ Users \ USER \

我的控制器是

namespace App\Http\Controllers;

use Illuminate\Http\Request;
//use App\Http\Controllers\Controller;
use App\users_signedup;



class UsersSignedupController extends Controller
{


    public function signup(Request $request)
    {

        return view('signup');
    }


    public function postUserSignupDetails(Request $request)
    {

        $validatedData = $request->validate([
            'state_id' => 'required',
            'local_govt_id' => 'required',
            'first_name' => 'required',
            'surname' => 'required',
            'phone_number' => 'required',
            'date_of_birth' => 'required',
            'title' => 'required',
            'education_level_id' => 'required|numeric',
            'discipline' => 'required',
            'res_address' => 'required',
            'work_address' => 'required',
            'occupation' => 'required',
            'gender' => 'required|numeric',
            'marital_status' => 'required|numeric',
            'make_donations' => 'numeric',
            'be_a_member' => 'numeric',
            'be_a_volunteer' => 'numeric',
            'about_you' => 'required',
            'email' => 'required|unique:users_signedups',
            'password' => 'required'

        ]);




    }

    public function store(Request $request)
    {
       // $users_signedup = $request->session()->get('users_signedup');
        $userModel = new users_signedup;

        //$userModel = new UserModel;
//        dd($request->email);
//        $request->state_id
        $userModel->state_id = $request->state_id;
        $userModel->local_govt_id = $request->local_govt_id;
        $userModel->first_name = $request->first_name;
        $userModel->surname = $request->surname;
        $userModel->phone_number = $request->phone_number;
        $userModel->date_of_birth = $request->date_of_birth;
        $userModel->title = $request->title;
        $userModel->education_level_id = $request->education_level_id;
        $userModel->discipline = $request->discipline;
        $userModel->work_address = $request->work_address;
        $userModel->res_address = $request->res_address;
        $userModel->occupation = $request->occupation;
        $userModel->gender = $request->gender;
        $userModel->marital_status = $request->marital_status;
        $userModel->make_donations = $request->make_donations;
        $userModel->be_a_member = $request->be_a_member;
        $userModel->be_a_volunteer = $request->be_a_volunteer;
        $userModel->about_you = $request->about_you;
        $userModel->email = $request->email;
        $userModel->password = $request->password;
       $userModel->save();


        return redirect('/signup/success');
    }

我的模特是

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class users_signedup extends Model
{
    protected $guarded = [];
}

我的观点是

<form action="signup/store" method="post">
                            {{ csrf_field() }}
                            <div class="col-sm-6">
                                <div class="form-group">

                                    <select class="form-control"  name="state_id" placeholder="Choose your State of Residence" required>
                                        <option value="">Choose your State of Residence</option>
                                        <option value="1">Abia State</option>


                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">

                                    <select class="form-control"  name="local_govt_id" placeholder="Choose your Local Government Area" required>
                                        <option value="">Choose your Local Government Area</option>
                                        <option value="1">Umahia</option>

                                    </select>
                                </div>
                            </div>

                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="first_name" placeholder="First Name" required>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="surname" placeholder="Surname" required>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="phone_number" placeholder="Phone" required>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="date_of_birth" placeholder="Date of Birth" required>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">

                                    <select class="form-control"  name="title" placeholder="Title" required>
                                        <option value="">Choose your Title</option>
                                        <option value="Mr">Mr</option>
                                        <option value="Mrs">Mrs</option>
                                        <option value="Ms">Ms</option>
                                        <option value="Miss">Miss</option>
                                        <option value="Prof">Prof</option>
                                        <option value="Chief">Chief</option>
                                        <option value="Dr.">Dr.</option>
                                        <option value="Hon.">Hon.</option>



                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="marital_status" placeholder="Marital Status" required>
                                </div>
                            </div>

                            <div class="col-sm-6">
                                <div class="form-group">

                                    <select class="form-control"  name="education_level_id" placeholder="Education Level" required>
                                        <option value="">Choose your Education Level</option>
                                        <option value="none">No Education</option>
                                        <option value="primary">Primary</option>
                                        <option value="secondary">Secondary</option>
                                        <option value="tertiary">Tertiary</option>

                                    </select>
                                </div>
                            </div>

                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="discipline" placeholder="Discipline" required>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <textarea class="form-control" name="res_address" data-gramm="true" spellcheck="false" data-gramm_editor="true" placeholder="Residential Address" required="required"></textarea>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <textarea class="form-control" name="work_address" data-gramm="true" spellcheck="false" data-gramm_editor="true" placeholder="Workplace Address" required="required">
                                        {{ route('signup') }}
                                        {{ csrf_field() }}

                                    </textarea>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="occupation" placeholder="Occupation" required>
                                </div>
                            </div>

                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="email" name="email" placeholder="Email" required>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input class="form-control" type="text" name="password" placeholder="Password" required>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    {{--<div class="g-recaptcha" data-sitekey="6LfXfnwUAAAAAIJW_OYS6viKZ-ydqSn5_-obFdys"></div>--}}
                                </div>
                            </div>



                            <div class="col-sm-12">
                                <div class="form-group">

                                </div>
                                <button class="sub-btn donate-btn more-btn hvr-shutter-out-horizontal">send</button>
                            </div>
                        </form>

3 个答案:

答案 0 :(得分:0)

您当前正在从请求对象中调用字段名称。正确的代码应为

$model->input = $request->input('input_name');

看看here了解更多信息。

答案 1 :(得分:0)

您应该尝试以下操作:

$inputData = $request->all()

users_signedup::create($inputData);

OR

DB::table('users_signedup')->insert($inputData);

答案 2 :(得分:0)

<option value="***enter default value***">Choose your Local Government Area</option> 

您需要强制用户选择一个选项,或者可以分配默认值。
您还可以在数据库中将该字段设置为可空或使用默认值。
请检查有关空选项的所有输入。除此以外,您的整个代码对我来说似乎还不错。