drpmazn9021 2018-05-07 01:06
浏览 46
已采纳

Laravel ajax只传递了一半的数据

I'm trying to save data to database with ajax, i am using the exact same way for to ajax request one of them working another one no.

here is my ajax that doesn't work.

script

<script>
  $( document ).ready( function() {
    $("#modalsave1").click(function(e){
      e.preventDefault();
    $.ajax({
      type: "post",
      url: "{{ url('admin/addnewsuboptionprodcreat') }}",
      data: {
        '_token': $('input[name=_token]').val(),
        'subopttitle': $('input[name=subopttitle]').val(),
        'opt_id': $('#opt_id').val(),
        'subopt_price': $('#subopt_price').val(),
        'optstat_id': $('#optstat_id').val(),
      },
      success: function (data) {
          console.log(data);
      },
      error: function (data) {
          console.log('Error:', data);
      }
    });
  });
  });
</script>

blade form

{{Form::open()}}

// get parent
{{ Form::label('opt_id', 'Parent Option') }}
<select class="form-control" id="opt_id" name="opt_id">
  <option value="">Select</option>
  @foreach($options as $option)
    <option value="{{ $option->id }}">{{ $option->title }}</option>
  @endforeach
</select>

//get title
{{ Form::label('subopttitle', 'Name') }}
{{ Form::text('subopttitle', null, array('id' => 'subopttitle', 'class' => 'form-control')) }}

//get price
{{ Form::label('subopt_price', 'Price') }}
{{ Form::text('subopt_price', null, array('id' => 'subopt_price', 'class' => 'form-control')) }}

//get status
{{ Form::label('optstat_id', 'Include filters?') }}
<select class="form-control" id="optstat_id" name="optstat_id">
  <option value="">Select</option>
  @foreach($statuses as $status)
    <option value="{{ $status->id }}">{{ $status->title }}</option>
  @endforeach
</select>

//save it
<button type="submit" id="modalsave1" class="modalsave1 btn btn-primary">Save</button>

{{Form::close()}}

and here is my controller

public function addnewsuboptionprodcreat(Request $reqs)
    {
      $add = Suboption::create([
          'title' => $reqs->subopttitle,
          'option_id' => $reqs->opt_id,
          'price' => $reqs->subopt_price,
          'status_id' => $reqs->optstat_id,
      ]);

      if($add){
        Session::flash('success', 'Your Sub-option saved successfully.');
      }else{
        Session::flash('danger', 'Your Sub-option didn\'t save successfully.');
      }
    }

route

  Route::post('/addnewsuboptionprodcreat', 'ProductController@addnewsuboptionprodcreat')->name('addnewsuboptionprodcreat');

I wonder, exact same method just different inputs works and this not.

Error

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mysite`.`suboptions`, CONSTRAINT `suboptions_option_id_foreign` FOREIGN KEY (`option_id`) REFERENCES `options` (`id`)) (SQL: insert into `suboptions` (`title`, `price`, `updated_at`, `created_at`) values (Yellow, 5000, 2018-05-07 07:57:16, 2018-05-07 07:57:16))

as you see in last line of the error i only get title price and timestamps but nothing for parent and status

any idea?

  • 写回答

2条回答 默认 最新

  • dongquepao8653 2018-05-07 01:08
    关注

    Add option_id and status_id into protected $fillable in Suboption Model

    class Suboption
    {
        protected $fillable = ['title','price','option_id','status_id'];
        ....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决