【问题标题】:data is not submitting into db:laravel-5.8数据未提交到 db:laravel-5.8
【发布时间】:2019-11-18 21:52:32
【问题描述】:

我是 laravel 的新手,并试图将数据保存到数据库中,但没有任何反应。我只是想把它保存到数据库中。

控制者:

       public function store(Request $request)
       {
        $request->validate([
            'date' => 'required',
            'start_time' => 'required',
            'end_time' => 'required',
            'time_slot' => 'required',
        ]);

        Form::create(request(['date', 'start_time', 'end_time', 'time_slot']));

        $data->save();
        return view('form.index');
       }

刀片文件:

       <table class="table table-hover mt-5">
            <thead>
              <tr>
                <th scope="col">Date</th>
                <th scope="col">Start Time</th>
                <th scope="col">End Time</th>
                <th scope="col">Time Slice</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <th>
                  <input type="date" name="date">
                </th>
                <td>
                  <input type="text" id="datetimepicker" name="start_time" />
                </td>
                <td>
                  <input type="text" id="datetimepicker" name="end_time" />
                </td>
                <td>
                  <select name="time_slot">
                    <option value="1">15 min</option>
                    <option value="2">30 min</option>
                    <option value="3">45 min</option>
                    <option value="4">1 hr</option>
                  </select>
                </td>
              </tr>
            </tbody>
          </table>

          <button type="submit" type="button" class="btn btn-primary">Add Data</button>

我正在为它使用资源路由。

【问题讨论】:

  • $data-&gt;save(); 你在你的代码中看到任何$data 变量吗?

标签: laravel eloquent laravel-5.8


【解决方案1】:

您有 2 个选项可以将其保存在数据库中:
1---------------

public function store(Request $request)
   {
    $request->validate([
        'date' => 'required',
        'start_time' => 'required',
        'end_time' => 'required',
        'time_slot' => 'required',
    ]);

    $form = new Form(); //make new object from your model
    $form->date = $request['date'];  //each of the DB field fill with $request fields
    $form->start_time = $request['start_time'];
    $form->end_time = $request['end_time'];
    $form->time_slot = $request'time_slot'];
    $form->save(); //Save this to your DB

    return view('form.index');
   }

2-------------------推荐
这样你需要在你的模型中做一些事情
型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Form extends Model
{
    protected $table = "forms"; //table name in your DB
    protected $fillable = ['date','start_date','end_date','time_slot']; //your fields in DB table that you want to fill them
}

控制器

public function store(Request $request)
   {
    $request->validate([
        'date' => 'required',
        'start_time' => 'required',
        'end_time' => 'required',
        'time_slot' => 'required',
    ]);

    $form = new Form(); //make new object from your model
    $form->create($request->all()); //NOTE:your fields in view should be the same name with your fields in DB, i mean for example you should get the start_date from an input that it's name is start_date, <input name="start_date">  

    return view('form.index');
   }

希望对你有帮助!

【讨论】:

  • 以哪种方式?第一种还是第二种方式?
  • 仍然没有保存
  • 现在一般错误:1364 字段“日期”没有默认值
  • 这意味着您的表单(视图)不会使用 $request 将“日期”值发送到您的控制器,并且 DB 需要保存日期值,检查您的表单和来自的 $request 值查看
  • 请将您的模型、视图、控制器、路由发送到我的邮箱,reza.sh_rs@yahoo.com
【解决方案2】:
    public function store(Request $request)
    {
            $request->validate([
                'date' => 'required',
                'start_time' => 'required',
                'end_time' => 'required',
                'time_slot' => 'required',
            ]);

            Form::create($request->all());

            return view('form.index');
     }

刀片文件:

        <form method="post" action="{{ route('form.store' }}">
          {{ csrf_field() }}
          <table class="table table-hover mt-5">
            <thead>
              <tr>
                <th scope="col">Date</th>
                <th scope="col">Start Time</th>
                <th scope="col">End Time</th>
                <th scope="col">Time Slice</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <th>
                  <input type="date" name="date">
                </th>
                <td>
                  <input type="text" id="datetimepicker" name="start_time" />
                </td>
                <td>
                  <input type="text" id="datetimepicker" name="end_time" />
                </td>
                <td>
                  <select name="time_slot">
                    <option value="1">15 min</option>
                    <option value="2">30 min</option>
                    <option value="3">45 min</option>
                    <option value="4">1 hr</option>
                  </select>
                </td>
              </tr>
            </tbody>
          </table>

          <button type="submit" class="btn btn-primary">Add Data</button>
          </form>

【讨论】:

  • 在你的问题中加入带有&lt;form&gt;标签的代码
  • 控制器完整代码 paste.ofcode.org/H4YkRwVZAQEkVHb8LL6E3d
  • 检查我更新了我的答案只是你复制粘贴到你的 IDE 中并运行它
猜你喜欢
  • 1970-01-01
  • 2019-06-18
  • 2012-12-15
  • 2013-05-26
  • 2011-03-21
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多