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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀