我是新来的laravel, 我正在使用ajax插入一些数据来建模“学生”,
$('#form-insert').on('submit', function(e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
e.preventDefault();
var data = $(this).serialize();
var url = $(this).attr('action');
var method = $(this).attr('method');
// console.log(method);
$.ajax({
type: method,
url: url,
data: data,
dataTy: 'json',
success: function(data) {
console.log(data);
}
});
});
这是我的查看表
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Add new Student</h4>
</div>
<form class="" action="{{route('store')}}" method="POST" id="form-insert">
<div class="modal-body">
<div class="col-md-12">
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" name="firstname" value="" class="form-control">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" name="lastname" value="" class="form-control">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="gender">Gender</label>
<select class="form-control" name="gender">
<option value="">Choose Gender</option>
<option value="laki-laki">Laki-laki</option>
<option value="perempuan">Perempuan</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-large float-left btn-info">submit</button>
<button type="submit" class="btn btn-large float-right btn-default" data-dismiss="modal">close</button>
</div>
</form>
</div>
</div>
</div>
这是我的模范生
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class student extends Model
{
protected $fillabel = ['firstname','lastname','gender'];
}
这是我的路线
Route::get('/', function () {
return view('welcome');
});
Route::prefix('ajax')->group(function(){
Route::get('/','AjaxController@index')->name('readdata');
Route::get('/readstudent','AjaxController@readdata')->name('readdata');
Route::post('/store','AjaxController@store')->name('store');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
这是我的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Response;
use App\student;
class AjaxController extends Controller
{
public function index(){
return view('ajax.index');
}
public function readdata(){
$data = student::all();
// return response($data);
return view('ajax.studentlist',compact('data'));
}
function store(Request $request){
if($request->ajax()){
$student = student::create($request->all());
return response($student);
}
}
}
,我尝试使用此控制器代码来插入数据,但是它不起作用,并给我错误500,我不知道为什么..但是当我更改像这样的contoller代码时
function store(Request $request){
if($request->ajax()){
$student = new student;
$student->firstname = $request->firstname;
$student->lastname = $request->lastname;
$student->gender = $request->gender;
$student->save();
// $student = student::create($request->all());
return response($student);
}
}
它工作正常,我只想知道以前的代码有什么问题。
答案 0 :(得分:0)
应该在模型中将$ fillabel写为$fillable