Laravel ajax只传递了一半的数据

时间:2018-05-07 01:06:35

标签: php ajax laravel

我正在尝试使用ajax将数据保存到数据库,我使用完全相同的方式来处理ajax请求其中一个工作另一个没有。

这是我的ajax不起作用。

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

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()}}

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'); 有效,而不是。

错误

just different inputs

正如您在错误的最后一行中看到的那样,我只获得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)) titlepricetimestampsparent

任何想法?

2 个答案:

答案 0 :(得分:3)

子选项模型添加以下受保护的$ guarded

class Suboption
{
    /**
    * The attributes that aren't mass assignable.
    *
    * @var array
    */
    protected $guarded = [];
}

答案 1 :(得分:1)

option_idstatus_id添加到protected $fillable模型中的Suboption

class Suboption
{
    protected $fillable = ['title','price','option_id','status_id'];
    ....
}