Laravel form not submitting data to database

2019-09-12 12:39发布

问题:

For some reason, my form isn't submitting any data to the database, but it looks fine from where I'm standing and the database can call the information to the form fine.

Since there's a lot of information for people to submit, I'm making the profile details not part of the login process. Still unfamiliar with how Laravel does these but I roughly get the process now that I've been fiddling.

One thing I'm wondering, is there a specific syntax for forms to write to the database, should I be naming the database's respective table names in the form? Or is that part of the Controller?

Should I be using Form Model Binding? It's a little hard to find information on that that's for the current version of Laravel though.

What am I missing?

//Route::get('NewUser', 'UserEntryController@create');
Route::post('NewUser', 'UserEntryController@UserForm');
//Route::get('NewUser', 'UserEntryController@create')->name('NewUser');
Route::post('NewUser', 'UserEntryController@UserForm')->name('submit');

Controller:

<?php

namespace App\Http\Controllers;

use App\UserEdit;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;

class UserEntryController extends Controller
{

   protected function create()
    {
        $id = UserEdit::find(715)->toArray();
        return view('NewUser', compact('id'));
        //return $array;
    }

    public function UserForm(Request $request) {
        $email = $request['email'];
        $first_name = $request['first_name'];
        $password_hint = $request['password_hint'];
        $last_name = $request['last_name'];

        $user = UserEdit::find(715)->first();
        $user->email = $email;
        $user->First_Name = $first_name;
        $user->Last_Name = $last_name;
        $user->Password_Hint = $password_hint;

        $user->save();

        $id = UserEdit::find(715)->toArray();
        return view('NewUser', compact('id'));
    }

}

Blade:

@extends('layout')


@section('content')
    <h1> Add Your Information {{ $id['name'] }}</h1>
    <div class="row">
        <div class="col-md-6">
            <h3>Edit</h3>
            <form action="{{ route('submit') }}" method="post">
                <div class="form-group">
                {{ csrf_field() }}
                    <label for="email">Your E-Mail</label>
                    <input class="form-control" type="text" name="email" id="email">
                </div>
                <div class="form-group">
                {{ csrf_field() }}
                    <label for="first_name">Your First Name</label>
                    <input class="form-control" type="text" name="first_name" id="first_name">
                </div>
                <div class="form-group">
                {{ csrf_field() }}
                    <label for="last_name">Your Last Name</label>
                    <input class="form-control" type="text" name="last_name" id="last_name">
                </div>
                <div class="form-group">
                {{ csrf_field() }}
                    <label for="password_hint">Your Password Hint</label>
                    <input class="form-control" type="text" name="password_hint" id="password_hint">
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
                <input type="hidden" name="_token" value="{{ Session::token() }}">
            </form>
        </div>
    </div>

    @foreach ($id as $key=>$value)
        {{ $value }}<br>
    @endforeach
@stop

回答1:

You should use $request->get('email') instead of $request['email'] and the same for everything else you want from the request, and I don't think you have to use ->first() when using ->find



回答2:

There could be something wrong with the _token field, since there are five csrf fields in the form.

Try to remove this line

<input type="hidden" name="_token" value="{{ Session::token() }}">

and just leave one {{ csrf_field() }} in the form.