嗨,有人可以帮我吗?请尝试添加到“ schools-list.store”路由时,我遇到了添加数据的问题,当我点击“保存”按钮时,该路由重定向到“ next”页面”而不是“ schools-list.store”或“ ajax”怎么了?
schools.blade.php
<form id="school-forms" method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="col-md-3" for="province_name">province_name</label>
<div class="col-md-9">
<select id="province_name" name="province_id" class="form-control col-md-12" required>
@foreach($province_names as $province_name)
<option value="{{ $province_name->id }}">{{ $province_name->province_name }}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="city_name">city_name</label>
<div class="col-md-9">
<select id="city_name" name="city_id" class="form-control col-md-12" required>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="center_name">center_name</label>
<div class="col-md-9">
<select id="center_name" name="center_id" class="form-control col-md-12" required>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="national_number_founder">national_number_founder</label>
<div class="col-md-9">
<input type="text" id="national_number_founder" name="national_number_founder" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="school_name">school_name</label>
<div class="col-md-9">
<input type="text" id="school_name" name="school_name" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="status">status</label>
<div class="col-md-9">
<div class="radio">
<label>
<input type="radio" name="status" id="1" value="1"> active
</label>
<label>
<input type="radio" name="status" id="0" value="0"> deactive
</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="gender">gender</label>
<div class="col-md-9">
<div class="radio">
<label>
<input type="radio" name="gender" id="male" value="1"> male
</label>
<label>
<input type="radio" name="gender" id="female" value="2"> female
</label>
<label>
<input type="radio" name="gender" id="female" value="3"> all
</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="note">note</label>
<div class="col-md-9">
<div class="checkbox">
<label>
<input type="checkbox" name="note[]" value="34"> 34
</label>
<span class="col-sm-12"></span>
<label>
<input type="checkbox" name="note[]" value="35"> 35
</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="type">type</label>
<div class="col-md-9">
<input type="text" id="type" name="type" class="form-control col-md-12">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="col-md-3" for="file_number">file_number</label>
<div class="col-md-9">
<input type="text" id="file_number" name="file_number" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="phone">phone</label>
<div class="col-md-9">
<input type="text" id="phone" name="phone" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="address">address</label>
<div class="col-md-9">
<input type="text" id="address" name="address" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="postal_code">postal_code</label>
<div class="col-md-9">
<input type="text" id="postal_code" name="postal_code" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="building">building</label>
<div class="col-md-9">
<input type="text" id="building" name="building" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="area">area</label>
<div class="col-md-9">
<input type="text" id="area" name="area" class="form-control col-md-12">
</div>
</div>
<div class="form-group">
<label class="col-md-3" for="date_establishment">date_establishment</label>
<div class="col-md-9">
<input type="text" id="date_establishment" name="date_establishment" class="form-control col-md-12">
</div>
</div>
</div>
</div>
<div class="row">
<div class="wizard-buttons">
<button id="save-activity" type="submit" class="btn btn-next2 col-md-4 btn-lg">
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span> save & continue
</button>
</div>
</div>
</form>
Ajax
$("#school-forms").submit(function (event) {
event.preventDefault();
province_id = $('#province_id').val();
city_id = $('#city_id').val();
center_id = $('#center_id').val();
national_number_founder = $('#national_number_founder').val();
school_name = $('#school_name').val();
status = $('#status').val();
gender = $('#gender').val();
note = $('#note').val();
type = $('#type').val();
file_number = $('#file_number').val();
phone = $('#phone').val();
address = $('#address').val();
postal_code = $('#postal_code').val();
building = $('#building').val();
date_establishment = $('#date_establishment').val();
$.post("{{ route('schools-list.store') }}", {province_id:province_id, city_id:city_id, center_id:center_id, national_number_founder:national_number_founder, school_name:school_name, status:status, gender:gender, note:note, type:type, file_number:file_number, phone:phone, address:address, postal_code:postal_code, building:building, date_establishment:date_establishment}, function (data_school) {
$('#province_id').val('');
$('#city_id').val('');
$('#center_id').val('');
$('#national_number_founder').val('');
$('#school_name').val('');
$('#status').val('');
$('#gender').val('');
$('#note').val('');
$('#type').val('');
$('#file_number').val('');
$('#phone').val('');
$('#address').val('');
$('#postal_code').val('');
$('#building').val('');
$('#date_establishment').val('');
console.log(event);
}, 'json');
});
数据库
Schema::create('schools', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('province_id')->unsigned();
$table->integer('city_id')->unsigned();
$table->integer('center_id')->unsigned();
$table->string('national_number_founder');
$table->string('school_name');
$table->boolean('status');
$table->boolean('gender');
$table->integer('note');
$table->string('type');
$table->string('file_number');
$table->string('phone');
$table->string('address');
$table->string('postal_code');
$table->string('building');
$table->string('area');
$table->string('date_establishment');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('province_id')->references('id')->on('provinces');
$table->foreign('city_id')->references('id')->on('cities');
$table->foreign('center_id')->references('id')->on('centers');
});
}
SchoolListController.php
class SchoolsListController extends Controller
{
public function index()
{
$schools_lists = SchoolsList::where('user_id', Auth::user()->id)->latest()->paginate(25);
return view('Admin.submit-information.all', compact('schools_lists'));
}
public function read_schools()
{
$schools = SchoolsList::all();
return response()->json($schools);
}
public function findIDProvince(Request $request)
{
$data = City::select('city_name', 'id')->where('province_id', $request->id)->take(100)->get();
return response()->json($data);
}
public function findIDCity(Request $request)
{
$data = Center::select('center_name', 'id')->where('city_id', $request->id)->take(100)->get()->toArray();
return response()->json($data);
}
public function create()
{
$province_names = Province::all();
return view('Admin.submit-information.create', compact('province_names'));
}
public function store(Request $request)
{
if($request->ajax()) {
$school = new SchoolsList();
$school->user_id = auth()->user()->id;
$school->province_id = $request->province_id;
$school->city_id = $request->city_id;
$school->center_id = $request->center_id;
$school->national_number_founder = $request->national_number_founder;
$school->school_name = $request->school_name;
$school->status = $request->status;
$school->gender = $request->gender;
$school->note = $request->note;
$school->type = $request->type;
$school->file_number = $request->file_number;
$school->phone = $request->phone;
$school->address = $request->address;
$school->postal_code = $request->postal_code;
$school->building = $request->building;
$school->date_establishment = $request->date_establishment;
$school->save();
return response()->json(['data_school' => $request->all(), 'id' => $school->id]);
}
}
}
web.php
$this->get('/findIDCity', 'SchoolsListController@findIDCity');
$this->get('/findIDProvince', 'SchoolsListController@findIDProvince');
$this->resource('schools-list', 'SchoolsListController');
当我单击“保存”时,我在网络中收到此405错误。