在laravel中,数据未插入数据库

在laravel中,数据未插入数据库,laravel,eloquent,Laravel,Eloquent,我是新来拉威尔和做项目使用拉威尔框架请帮助我解决这个问题 数据未插入表中 routes.php Route::post('report/save','TestingController@store'); <?php namespace App\Model; use DB; use Illuminate\Database\Eloquent\Model; class Testing extends Model { protected $table = 'testing_repor

我是新来拉威尔和做项目使用拉威尔框架请帮助我解决这个问题

数据未插入表中

routes.php

Route::post('report/save','TestingController@store');
<?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',
];
}
测试控制器

 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);


    }
模型->测试.php

Route::post('report/save','TestingController@store');
<?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',
];
}
{{$data->location\u name}
@endforeach
{{trans('message.table.casting_date')}*
{{trans('message.table.testing_date')}*
{{trans('message.form.gradeofconcrete')}
{{trans('message.form.testing_age')}
{{trans('message.table.sno')}
{{trans('message.table.wt')}
{{trans('message.table.load')}
复合强度(单位:兆帕)
平均强度
{{trans('message.table.remark')}
{{trans('message.table.action')}
{{trans('message.table.machine')}
{{trans('message.table.calibrated')}
7天的
27天的
添加自定义项目


{{trans('message.form.submit')}
问题:

当我点击提交按钮时,什么都没有发生

任何帮助都将不胜感激


这是我的看法

          <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>


{{trans('message.testing.client')}
选择客户端
@foreach($customerData作为$data)
{{$data->name}
@endforeach
{{trans('message.form.from_location')}
@foreach($locData作为$data)
>{{$data->location_name}
@endforeach
{{trans('message.table.casting_date')}*
{{trans('message.table.testing_date')}*
{{trans('message.form.gradeofconcrete')}
{{trans('message.form.testing_age')}
{{trans('message.table.sno')}
{{trans('message.table.wt')}
{{trans('message.table.load')}
复合强度(单位:兆帕)
平均强度
{{trans('message.table.remark')}
{{trans('message.table.action')}
{{trans('message.table.machine')}
{{trans('message.table.calibrated')}
7天的
27天的
添加自定义项目


{{trans('message.form.submit')}
要存储数据,我将您的html元素名称命名为db字段,并使用简单的:

$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而不是原始数据库查询。

只有在您使用有说服力的查询时,才能使用您的模型。因此,根据您的代码,您不使用该模型,因为您使用的是查询生成器

 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);


    }
我有两个建议:

  • 检查您的日期格式“date('Y-m-d')”,确保mysql允许这样做
  • 将您的$request->from_stk_loc更改为$request['from_stk_loc'],如果代码正确,则更改其余部分

  • 请再试一次,让我知道这是否有帮助。

    首先,我同意大家推荐Elounce的ORM功能而不是“原始”DB查询

    至于一个可能的解释:您正在尝试将“report_date”插入一个不可填充的列中,该列在此表中可能不可为空

    因此,请在您的模型中尝试以下方法:

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

    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;    
    
        }
    

    通过在查看页面中添加
    最终解决了此问题

    请确保模型中的可填充字段均为提及的相应字段,正确无多余空间。在您的模型中,您没有提到“报告日期”字段

    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'
    ];
    

    OP使用了不同的列名,因此这不起作用,
    债务人编号
    作为
    客户id
    插入,是否有任何错误消息?试着一步一步地使用我的代码,不要只是复制/粘贴…让我们看看你的变量$test\u当前有什么详细信息?在插入之前,通过命令dd($test_details),您是否得到任何错误?请在模型中添加$guarded=['id']。如果将id设置为自动递增否。。它既不显示dd($test_details)也不显示错误显示html代码怎么样?为了让我们更好地了解。谢谢,请尝试将它添加到$userId和store()之间的控制器中,并将结果复制粘贴到此处。dd($request->all());没有错误,即使定义了重定向路径,它也会加载相同的页面。您的表需要它吗?换句话说,“report_date”是否设置为空?这或许可以解释这一点。您的模型阻止发送“report_date”,而您的表要求发送它。将“报告日期”添加到您的