我的代码正常工作正常,但是当我使用ajax时,我在保存多个输入时遇到问题,我收到此消息
“SQLSTATE[01000]:警告:1265 第 1 行的列“hotel_id”的数据被截断(SQL:插入 hotel_tour
(hotel_id
, tour_id
) 值 (5,2, 6))"
我使用引导选择输入 https://silviomoreto.github.io/bootstrap-select/
查看:
<tr>
{!! Form::open(['route' => 'tour.store', 'method' => 'POST', 'id'=>'create-form','class'=>'form-horizontal row-border', 'enctype'=>'multipart/form-data']) !!}
<td>{{--Hotel--}}
{!! Form::select('hotel_id[]', $hotels, null,
['title' => trans('panel.hotels'),
'id'=>'hotel_id','class'=>'select input-lg dropdown-toggle form-control',
'multiple data-live-search="true"',])!!}
</td>
<td>{{--Two Bed --}}
{!! Form::text('two_bed_price',null,['class' => 'form-control'])!!}
</td>
<td>{{--One Bed --}}
{!! Form::text('one_bed_price',null,['class' => 'form-control'])!!}
</td>
<td>{{--Child With Bed --}}
{!! Form::text('child_with_bed_price',null,['class' => 'form-control'])!!}
</td>
<td>{{--Child No Bed --}}
{!! Form::text('child_no_bed_price',null,['class' => 'form-control'])!!}
</td>
<td> {{--Labels--}}
{!! Form::select('label_id[]', $labels, null,
['title' => trans('panel.labels'),
'id'=>'label_id','class'=>'select input-lg dropdown-toggle form-control',
'multiple data-live-search="true"',
])!!}
</td>
<td>{{--Order--}}
{!! Form::number('ordered',null,['class' => 'form-control'])!!}
</td>
<td> {{--Status--}}
{{-- get config from config file with config() function --}}
{!! Form::select('status',config('system.status'),'published',['class' => 'form-control input-lg']) !!}
</td>
<td> {{--Submit & Cancel--}}
{!! Form::submit(trans('panel.submit'),['id'=>'create','class'=>'btn btn-lg btn-info'])!!}
</td>
<td>
<a id="remove-tour" class="btn btn-lg btn-warning">-</a>
</td>
{{--Package--}}
{!! Form::hidden('package_id',$package->id)!!}
{!! Form::close() !!}
</tr>
js:
$("#create-form").on('submit', function(e){
var _token = $('input[name="_token"]').val();
e.preventDefault(e);
var url = "{{route('tour.store')}}";
var hotel_id = $('#hotel_id').val();
var label_id = $('#label_id').val();
var values = $(this).serialize() +'&hotel_id=' + hotel_id
+'&label_id=' + label_id;
$.ajax({
type:"POST",
url:url,
data:values,
dataType: 'json',
headers: {
'X-CSRF-TOKEN':_token
},
success: function(data){
console.log(data);
},
error: function(data){
}
});
});
Controller
public function store(Request $request)
{
//Validation
Validator::make($request->all(), [
'package_id' => 'required|max:10|numeric',
'hotel_id' => 'required|max:10',
'status' => 'required|max:191',
])->validate();
$data = array_add($request->all(), 'user_id', Auth::user()->id); //add user id to request
//Save Item
if ( $item =Tour::create($data)) {
if(
$item->hotels()->sync($request->input('hotel_id'))
) {
$item->labels()->sync($request->input('label_id'));
$url = route('tour.edit', ['id' => $item->id]); //go to edit page
$message = trans('message.item-created', ['item' => 'Tour']); //translate message
Session::flash('message', $message);
return redirect($url);
型号
class Tour extends Model
{
protected $guarded = ['id','hotel_id','label_id'];
public $timestamps = false;
function package()
{
return $this->belongsTo(Package::class);
}
public function currency()
{
return $this->belongsTo(Currency::class);
}
public function labels()
{
return $this->belongsToMany(Label::class);
}
public function hotels()
{
return $this->belongsToMany(Hotel::class);
}
}
最佳答案
我认为你应该在 jquery 的 ajax 对象中添加此代码
contentType: false,
processData: false,
关于jquery - Laravel在ajax中出现mysql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48675920/