数据未提交到数据库

时间:2019-01-24 19:12:35

标签: laravel eloquent laravel-routing laravel-5.7

我正在建立联系页面,但是表单数据没有保存到数据库中。有什么解决方案?

ContactController.php

public function contact()
{
    if($request->isMethod('post'))
    {
        $data = $request->all();
    }
    $contact = new Contact;
    $contact->name = $data['contact_name'];
    $contact->email = $data['contact_email'];
    $contact->subject = $data['contact_subject'];
    $contact->body = $data['description'];
    $category->save();

    return redirect()->back()->with('flash_message_success', 
        'Your message has been sent successfully');
}

contact.blade.php

<form action="{{ url('/contact') }}" id="main-contact-form" class="contact-form row" name="contact-form" method="post">
     {{ csrf_field() }}
     <div class="form-group col-md-6">
         <input type="text" name="contact_name" class="form-control" required="required" placeholder="Name">
     </div>
     <div class="form-group col-md-6">
         <input type="email" name="contact_email" class="form-control" required="required" placeholder="Email">
     </div>
     <div class="form-group col-md-12">
         <input type="text" name="contact_subject" class="form-control" required="required" placeholder="Subject">
     </div>
     <div class="form-group col-md-12">
         <textarea name="description" id="message" required="required" class="form-control" rows="8" placeholder="Your Message Here"></textarea>
     </div>                        
     <div class="form-group col-md-12">
         <input type="submit" name="submit" class="btn btn-primary pull-right" value="Submit">
     </div>
</form>

路线

Route::get('contact', function(){
    return view('contact');
});

Route::post('contact', function(){
    return view('contact');
});

1 个答案:

答案 0 :(得分:0)

使用$contact->save();而不是$category->save();,并删除if语句(暂时):if($request->isMethod('post')) {

您的路线应为:

Route::post('contact', 'ContactController@contact')->name('contact');