简体   繁体   中英

Why is input data not being sent to the controller after submit?[LARAVEL]

I already looked for the same issue in another topics, but most most issues is in model($fillable) or in form 'name' attribute. I think the problem is in my . When I submit the values are not sent to controller.

Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class ContactCli extends Eloquent
{
    protected $fillable = ['name', 'email', 'phone', 'message', 'subject'];

    protected $table = 'contact_clis';
}

Controller

public function contactUSPost(Request $request){

        $contact = new ContactCli();

        $contact->name = $request->input('name');
        $contact->email = $request->input('email');
        $contact->phone = $request->input('phone');
        $contact->subject = $request->input('subject');
        $contact->message = $request->input('message');

        //dd($contact);
        $contact->save();
         return redirect()->babck()->with('success', 'Thanks for contacting us!');
}

Route

Route::group(['prefix' => '/', 'namespace' => 'Cms'], function(){

    Route::post('home', 'PagesController@contactUSPost')->name('contactus.store');
});

To reduces the code I will only put input 'name'. Form

<form name="sentMessage" class="text-white" id="contactForm" method="POST" action="{{route('contactus.store')}}" novalidate="novalidate" >

        {{ csrf_field() }}
          <div class="control-group">
            <div class="form-group floating-label-form-group controls mb-0 pb-2">
              <label>Nome</label>
              <input class="form-control bg-secondary text-white" id="name" type="text" placeholder="Nome" required="required" data-validation-required-message="Please enter your name." autocomplete="off">
              <p class="help-block text-danger"></p>
            </div>
          </div>

          <div class="form-group">
            <button type="submit" class="btn btn-primary btn-xl" id="sendMessageButton">Enviar</button>
          </div>
          </form>
</form>

When run dd()

ContactCli {#598 ▼
  #fillable: array:5 [▼
    0 => "name"
    1 => "email"
    2 => "phone"
    3 => "message"
    4 => "subject"
  ]
  #table: "contact_clis"
  #connection: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: array:5 [▼
    "name" => null
    "email" => null
    "phone" => null
    "subject" => null
    "message" => null
  ]
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

So, the error that I get is:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into contact_clis ( name , email , phone , subject , message , updated_at , created_at ) values (, , , , , 2019-09-10 12:46:55, 2019-09-10 12:46:55))

Values sent from form are being sent null.

You are missing the main attribute in input field which is name attribute. Without this you cannot send data thorugh form.

<input class="form-control" id="name" type="text" name="name" required="required" >

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