在laravel数据中没有插入数据库

时间:2018-04-30 12:50:13

标签: laravel eloquent

我是laravel的新手,使用laravel框架做项目请帮我解决这个问题。

数据未插入表格。

routes.php文件

Route::post('report/save','TestingController@store');

TestingController

 public function store(Request $request){
        $userId = \Auth::user()->id;
        $this->validate($request, [
            'from_stk_loc' => 'required',
            'testing_date' => 'required',
            'casting_date' => 'required',
            'debtor_no' => 'required',
            'concrete_grade' => 'required',
            'testing_age' => 'required',
        ]);

        $test_details['client_id'] = $request->debtor_no;
        $test_details['location'] = $request->from_stk_loc;
        $test_details['casting_date'] = $request->casting_date;
        $test_details['testing_date'] = $request->testing_date;
        $test_details['concrete_grade'] = $request->concrete_grade;
        $test_details['testing_age'] = $request->testing_age;
        $test_details['report_date'] = date('Y-m-d');

        $test_detailsId = DB::table('testing_report')->insert($test_details);


    }

模型 - > Testing.php

<?php

namespace App\Model;
use DB;
use Illuminate\Database\Eloquent\Model;

class Testing extends Model
{
    protected $table = 'testing_report';
    protected $fillable = [
    'client_id',
    'location',
    'casting_date',
    'testing_date',
    'concrete_grade',
    'testing_age',
];
}

这是我的表单代码,您可以查看此内容并找到解决方案。

      <div class="col-md-3">
          <label for="exampleInputEmail1">{{ trans('message.testing.client') }}</label>
            <select class="form-control select2" name="client_id" id="client_id">
              <option value="all">Select client</option>
              @foreach($customerData as $data)
          <option value="{{$data->debtor_no}}">{{$data->name}}</option>
        @endforeach
            </select>
      </div>
        <div class="col-md-3">
      <div class="form-group">
          <label for="examplefromlocation">{{ trans('message.form.from_location') }}</label>
            <select class="form-control select2" name="from_stk_loc" id="from_stk_loc">
            @foreach($locData as $data)
              <option value="{{$data->loc_code}}" <?= ($data->inactive =="1" ? 'selected':'')?>>{{$data->location_name}}</option>
            @endforeach
            </select>
      </div>
    </div>

    <div class="col-md-3">
      <div class="form-group">
        <label>{{ trans('message.table.casting_date') }}<span class="text-danger"> *</span></label>
        <div class="input-group date">
          <div class="input-group-addon">
            <i class="fa fa-calendar"></i>
          </div>
          <input class="form-control" id="datepicker" type="text" name="casting_date">
        </div>
        <!-- /.input group -->
      </div>
  </div>
    <div class="col-md-3">
      <div class="form-group">
        <label>{{ trans('message.table.testing_date') }}<span class="text-danger"> *</span></label>
        <div class="input-group date">
          <div class="input-group-addon">
            <i class="fa fa-calendar"></i>
          </div>
          <input class="form-control" id="datepicker1" type="text" name="testing_date">
        </div>
        <!-- /.input group -->
      </div>
    </div>

     <div class="col-md-3">
      <div class="form-group">
          <label for="examplefromlocation">{{ trans('message.form.gradeofconcrete') }}</label>
           <input type="text" placeholder="{{ trans('message.form.gradeofconcrete') }}" class="form-control valdation_check" name="concrete_grade" value="">
      </div>
    </div>

       <div class="col-md-3">
      <div class="form-group">
          <label for="examplefromlocation">{{ trans('message.form.testing_age') }}</label>
           <input type="text" placeholder="{{ trans('message.form.testing_age') }}" class="form-control valdation_check" name="testing_age" value="">
      </div>
    </div>
     <div class="row">
    <div class="col-md-12">
      <!-- /.box-header -->
      <div class="box-body no-padding">
        <div class="table-responsive">
        <table class="table table-bordered" id="purchaseInvoice">
          <tbody>

          <tr class="tbl_header_color dynamicRows">
            <th width="5%" class="text-center">{{ trans('message.table.sno') }}</th>
            <th width="10%" class="text-center">{{ trans('message.table.wt') }}</th>
            <th colspan="2" width="10%" class="text-center">{{ trans('message.table.load') }}</th>
            <th colspan="2" width="10%" class="text-center">Comp Strength in M. Pa</th>
            <th width="15%" class="text-center">Avg Strength</th>
            <th width="15%"  class="text-center">{{ trans('message.table.remark') }}</th>
            <th width="5%"  class="text-center">{{ trans('message.table.action') }}</th>
          </tr>
          <tr class="tbl_header_color dynamicRows">
            <th width="5%" class="text-center"></th>
            <th width="10%" class="text-center"></th>
            <th width="10%" class="text-center">{{ trans('message.table.machine') }}</th>
            <th width="10%" class="text-center">{{ trans('message.table.calibrated') }}</th>
            <th width="10%" class="text-center">7 Day's</th>
            <th width="10%" class="text-center">27 Day's</th>
            <th width="10%" class="text-center"></th>
            <th width="15%" class="text-center"></th>
            <th width="5%"  class="text-center"></th>
          </tr>
          <tr class="custom-item"><td class="add-row text-danger"><strong>Add Custom Item</strong></td><td colspan="8"></td></tr>

          </tbody>
        </table>
        </div>
        <br><br>
      </div>
    </div>
      <!-- /.box-body -->
      <div class="col-md-12">

        <a href="{{url('/sales/list')}}" class="btn btn-info btn-flat">{{ trans('message.form.cancel') }}</a>
        <button type="submit" class="btn btn-primary btn-flat pull-right" id="btnSubmit">{{ trans('message.form.submit') }}</button>
      </div>
</div>
   </form>

问题:

当我点击提交按钮时,根本没有发生任何事情。

任何帮助都将受到高度赞赏。

这是视图

          <div class="col-md-3">
              <label for="exampleInputEmail1">{{ trans('message.testing.client') }}</label>
                <select class="form-control select2" name="client_id" id="client_id">
                  <option value="all">Select client</option>
                  @foreach($customerData as $data)
              <option value="{{$data->debtor_no}}">{{$data->name}}</option>
            @endforeach
                </select>
          </div>
            <div class="col-md-3">
          <div class="form-group">
              <label for="examplefromlocation">{{ trans('message.form.from_location') }}</label>
                <select class="form-control select2" name="from_stk_loc" id="from_stk_loc">
                @foreach($locData as $data)
                  <option value="{{$data->loc_code}}" <?= ($data->inactive =="1" ? 'selected':'')?>>{{$data->location_name}}</option>
                @endforeach
                </select>
          </div>
        </div>

        <div class="col-md-3">
          <div class="form-group">
            <label>{{ trans('message.table.casting_date') }}<span class="text-danger"> *</span></label>
            <div class="input-group date">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <input class="form-control" id="datepicker" type="text" name="casting_date">
            </div>
            <!-- /.input group -->
          </div>
      </div>
        <div class="col-md-3">
          <div class="form-group">
            <label>{{ trans('message.table.testing_date') }}<span class="text-danger"> *</span></label>
            <div class="input-group date">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
              <input class="form-control" id="datepicker1" type="text" name="testing_date">
            </div>
            <!-- /.input group -->
          </div>
        </div>

         <div class="col-md-3">
          <div class="form-group">
              <label for="examplefromlocation">{{ trans('message.form.gradeofconcrete') }}</label>
               <input type="text" placeholder="{{ trans('message.form.gradeofconcrete') }}" class="form-control valdation_check" name="concrete_grade" value="">
          </div>
        </div>

           <div class="col-md-3">
          <div class="form-group">
              <label for="examplefromlocation">{{ trans('message.form.testing_age') }}</label>
               <input type="text" placeholder="{{ trans('message.form.testing_age') }}" class="form-control valdation_check" name="testing_age" value="">
          </div>
        </div>
         <div class="row">
        <div class="col-md-12">
          <!-- /.box-header -->
          <div class="box-body no-padding">
            <div class="table-responsive">
            <table class="table table-bordered" id="purchaseInvoice">
              <tbody>

              <tr class="tbl_header_color dynamicRows">
                <th width="5%" class="text-center">{{ trans('message.table.sno') }}</th>
                <th width="10%" class="text-center">{{ trans('message.table.wt') }}</th>
                <th colspan="2" width="10%" class="text-center">{{ trans('message.table.load') }}</th>
                <th colspan="2" width="10%" class="text-center">Comp Strength in M. Pa</th>
                <th width="15%" class="text-center">Avg Strength</th>
                <th width="15%"  class="text-center">{{ trans('message.table.remark') }}</th>
                <th width="5%"  class="text-center">{{ trans('message.table.action') }}</th>
              </tr>
              <tr class="tbl_header_color dynamicRows">
                <th width="5%" class="text-center"></th>
                <th width="10%" class="text-center"></th>
                <th width="10%" class="text-center">{{ trans('message.table.machine') }}</th>
                <th width="10%" class="text-center">{{ trans('message.table.calibrated') }}</th>
                <th width="10%" class="text-center">7 Day's</th>
                <th width="10%" class="text-center">27 Day's</th>
                <th width="10%" class="text-center"></th>
                <th width="15%" class="text-center"></th>
                <th width="5%"  class="text-center"></th>
              </tr>
              <tr class="custom-item"><td class="add-row text-danger"><strong>Add Custom Item</strong></td><td colspan="8"></td></tr>

              </tbody>
            </table>
            </div>
            <br><br>
          </div>
        </div>
          <!-- /.box-body -->
          <div class="col-md-12">

            <a href="{{url('/sales/list')}}" class="btn btn-info btn-flat">{{ trans('message.form.cancel') }}</a>
            <button type="submit" class="btn btn-primary btn-flat pull-right" id="btnSubmit">{{ trans('message.form.submit') }}</button>
          </div>
    </div>
       </form>

6 个答案:

答案 0 :(得分:0)

要存储数据,我会将您的html元素名称作为db字段并使用simple:

$testing = new Testing();
$testing->fill($request->all());
$testing->client_id = $request->your_client_id_from_source;
$testing->user_id = auth()->user()->id;
$testing->save();

因此,您将直接使用laravel ORM而不是原始数据库查询。

答案 1 :(得分:0)

您的模型只能在使用 Eloquent Queries 时使用。因此,根据您的代码,您不使用模型,因为您使用查询生成器

我有2条建议:

  1. 检查日期格式“date('Y-m-d')”确保mysql允许这样做。
  2. 将您的$ request-&gt; from_stk_loc更改为$ request ['from_stk_loc'],其余代码更改。
  3. 再试一次,如果有帮助请告诉我。

答案 2 :(得分:0)

首先,我同意所有人推荐Eloquent的ORM功能而不是原始的&#39;数据库查询。

至于可能的解释:您正在尝试插入不可填写的列:&#39; report_date&#39;,在此表中可能无法为空。

所以在你的模型中试试这个:

protected $fillable = [
  'client_id',
  'location',
  'casting_date',
  'testing_date',
  'concrete_grade',
  'testing_age',
  'report_date'
];

答案 3 :(得分:0)

试试这个:

use App\Model\Testing;

public function store(Request $request){
        $userId = \Auth::user()->id;
        $validator = Validator::make($request->all(), [
            'from_stk_loc' => 'required',
            'testing_date' => 'required',
            'casting_date' => 'required',
            'debtor_no' => 'required',
            'concrete_grade' => 'required',
            'testing_age' => 'required',
        ]);
        if( $validator->fails() )
        {
           return redirect()->back()->withInput()->withErrors($validator);
        } else {
            $testingObj = new Testing;
            $testingObj->client_id = $request->client_id;
            $testingObj->location = $request->location;
            $testingObj->casting_date = $request->casting_date;
            $testingObj->testing_date = $request->testing_date;
            $testingObj->concrete_grade = $request->concrete_grade;
            $testingObj->testing_age = $request->testing_age;
            $testingObj->testing_age = $request->testing_age;
            $testingObj->report_date = date('Y-m-d'); // try date('Y-m-d H:i:s') if datatype is datetime
            $testingObj->save();                    

            $test_detailsId = $testingObj->id;    

    }

答案 4 :(得分:0)

最后通过在视图页面中添加<input type="hidden" value="{{csrf_token()}}" name="_token" id="token">来解决此问题

答案 5 :(得分:0)

请确保在模型的可填写字段中注明了各自的字段,正确且无多余空间。在您的模型中,您没有提到report_date字段。

namespace App\Model;
use DB;
use Illuminate\Database\Eloquent\Model;

class Testing extends Model
{
    protected $table = 'testing_report';
    protected $fillable
    = [
        'client_id',
        'location',
        'casting_date',
        'testing_date',
        'concrete_grade',
        'testing_age',
    //add this to the end.
        'report_date'
];