Laravel inserting "null" into database from form input.

时间:2016-07-11 22:16:47

标签: php mysql laravel null

This is a strange problem i've run into. I have a form that takes in First, Last, Title, Email, and Phone which takes that data and inputs into a ContactPerson table I have set up in a database.

When I fill out the form, and hit submit, only the Title, Email, and Phone number get inserted correctly into the database. The first and last names get inserted as NULL.

Here's some of my code that I have using PHPstorm and Laravel 2.5.3

My Create a New Contact view:

<h1 style="text-align:center;">Add A New Contact Person</h1>

    <hr/>

    {!! Form::open(['url' => '/create']) !!}

    <span style="text-align:center;align-content:right;display:block; margin-right:auto;margin-left:auto;">

        <div class="form">
            {!! Form::label('first', 'First Name: ') !!}
            {!! Form::text('First', null, ['class' => 'form']) !!}
        </div>

        <div class="form">
            {!! Form::label('last', 'Last Name: ') !!}
            {!! Form::text('Last', null, ['class' => 'form']) !!}
        </div>

        <div class="form">
            {!! Form::label('title', 'Title: ') !!}
            {!! Form::text('Title', null, ['class' => 'form']) !!}
        </div>

        <div class="form">
            {!! Form::label('email', 'Email: ') !!}
            {!! Form::text('Email', null, ['class' => 'form']) !!}
        </div>

        <div class="form">
            {!! Form::label('phone', 'Phone: ') !!}
            {!! Form::text('Phone', null, ['class' => 'form']) !!}
        </div>

        {!! Form::submit('Submit', ['class' => 'btn btn-primary form']) !!}

    </span>
    {!! Form::close() !!}

Here is my controller called ResourceController I have created:

class ResourceController extends Controller
{
    public function resource()
    {
        $contacts = ContactPerson::all();

        return view('pages.resource', compact('contacts'));     
    }

    public function create()
    {
        return view('pages.create');
    }

    public function store(Requests\CreateNewContactRequest $request)
    {
        ContactPerson::create($request->all());

        return redirect('resource');
    }
}

Here's the validation I have set up in the CreateNewContactRequest class for the form:

public function rules()
{
    return [
        'First' => 'required|min:2',
        'Last' => 'required|min:1',
        'Title' => 'required|min:2',
        'Email' => 'required|Email|unique:users',
        'Phone' => 'required|numeric|min:9'
    ];
}

Here's what it looks like when I fill out/submit the form.

Creating new contact

After hitting submit it redirects to the database dump: after submitting

the view of the database after insertion: database

1 个答案:

答案 0 :(得分:0)

Looking at your dump details, your field names need to match your form names or visa versa, so your form names need to change to say:

   <div class="form">
        {!! Form::label('first', 'First Name: ') !!}
        {!! Form::text('First_Name', null, ['class' => 'form']) !!}
    </div>

    <div class="form">
        {!! Form::label('last', 'Last Name: ') !!}
        {!! Form::text('Last_Name', null, ['class' => 'form']) !!}
    </div>

EG for field name First_Name your input needs to be named the same name="First_Name"

Also making sure your fillable details are also correct

 protected $fillabel = ['First_Name','Last_Name','Title','Email','Phone'];