我想为数据供应商创建输入,但显示http://localhost:8000/vendor 500 (Internal Server Error)
和Add [nama_vendor] to fillable property to allow mass assignment on [App\Vendor]
时出错。我尝试在模型中添加nama_vendor
,但错误仍在显示。
这是我的控制器
public function store(Request $request)
{
Vendor::Create([
'nama_vendor' =>$request->nama_vendor,
'no_hp' => $request->no_hp,
'email'=> $request->email,
'alamat' => $request->alamat,
]);
return response()->json(['success'=>' Data Vendor Berhasil Disimpan.']);
}
这是我的观点
<script type="text/javascript">
$(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#saveBtn').click(function (e) {
e.preventDefault();
$(this).html('Menyimpan...');
$.ajax({
data: $('#vendorForm').serialize(),
url: "{{ route('vendor.store') }}",
type: "POST",
dataType: 'json',
success: function (data) {
$('#vendorForm').trigger("reset");
$('#ajaxModal').modal('hide');
$('#alertify-success').click();
table.draw();
},
error: function (data) {
console.log('Error:', data);
$('#saveBtn').html('Simpan Data');
}
});
});
</script>
这是我的模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Vendor extends Model
{
protected $fillabel = ['nama_vendor','no_hp','email','alamat'];
}
答案 0 :(得分:1)
首先,您的模型中有错字。不是 $ fillabel ,它应该是 $ fillable
将此更改为。
protected $fillable = ['nama_vendor','no_hp','email','alamat'];
还要解决以下问题
Vendor :: Create ..应该是Vendor :: create。
此外,您可以使用以下命令使所有属性可大量分配
protected $guarded = [];
答案 1 :(得分:1)
public function store(Request $request, Vendor $vendor)
{
$vendor->create([
'nama_vendor' =>$request->nama_vendor,
'no_hp' => $request->no_hp,
'email'=> $request->email,
'alamat' => $request->alamat,
]);
return response()->json(['success'=>' Data Vendor Berhasil Disimpan.']);
}
这应该可行,并且还可以修复类型“ $ fillable”
答案 2 :(得分:0)
在Vendor
模型中,您的拼写错误
放入$fillable
而不是$fillabel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Vendor extends Model
{
protected $fillable = ['nama_vendor','no_hp','email','alamat'];
}