我正在尝试将图片上传到我的laravel项目中。 没有错误我无法找到并且一直在尝试调试它。图像将保存到项目中的本地文件夹,图像,但它不会上传到mysql数据库。其他字段将插入到数据库中,只有图像部分不会上传。
这是我的控制器代码。
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($validator->passes()) {
$input = $request->all();
$input['image'] = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $input['image']);
Employee::create($input);
return response()->json(['success'=>'done']);
}
return response()->json(['error'=>$validator->errors()->all()]);
}
这就是我调用图像以保存的方式。
<?php $no=1; ?>
@foreach ($employees as $employee)
<tr class="post{{$employee->id}}">
<td>{{ $no++ }}</td>
<td>{{ $employee->name}}</td>
<td>{{ $employee->bank->name}}</td>
<td>{{ $employee->bankbranch->location}}</td>
<td>"{{ asset('images /'.$employee->photo)}}" width= "50px" height = "50px"</td>
<td>{{ $employee->created_at}}</td>
<td>
<a href="#" class="show-modal btn btn-info btn-sm" data-id="{{$employee->id}}" data-bank_id="{{$employee->bank->name}}" data-bankbranch_id="{{$employee->bankbranch->location}}" data-name="{{$employee->name}}" data-phone="{{$employee->phone}}" data-email="{{$employee->email}}" data-address="{{$employee->address}}" data-acname="{{$employee->acname}}" data-acnumber="{{$employee->acnumber}}" data-rnumber="{{$employee->rnumber}}" >
<i class="fa fa-eye"></i>
</a>
<a href="#" class="edit-modal btn btn-warning btn-sm" data-id="{{$employee->id}}" data-bank_id="{{$employee->bank->name}}" data-bankbranch_id="{{$employee->bankbranch->location}}" data-name="{{$employee->name}}" data-phone="{{$employee->phone}}" >
<i class="glyphicon glyphicon-pencil"></i>
</a>
<a href="#" class="delete-modal btn btn-danger btn-sm" data-id="{{$employee->id}}" data-bank_id="{{$employee->bank->name}}" data-bankbranch_id="{{$employee->bankbranch->location}}" data-name="{{$employee->name}}" data-phone="{{$employee->phone}}" >
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
@endforeach
这是保存图像的ajax调用。
$(document).on('click','.create-modal', function() {
$('#create').modal('show');
$('.form-horizontal').show();
$('.modal-title').text('Add Employee');
});
$('#employeeinput').on('submit',function(e){
e.preventDefault();
var form = document.forms.namedItem("employeeinput");
var formData = new FormData(form);
//var data = $(this).serialize();
var url = $(this).attr('action');
var post = $(this).attr('method');
$.ajax({
type: post,
url: url,
data: formData,
contentType: false,
processData: false,
dataTy: 'json',
success:function(data)
{
$('.error').remove();
$('#table').append("<tr class='post" + data.id + "'>"+
"<td>" + data.id + "</td>"+
"<td>" + data.name + "</td>"+
"<td>" + data.photo + "</td>"+
"<td>" + data.bank_name + "</td>"+
"<td>" + data.bankbranch_location + "</td>"+
"<td>" + data.created_at + "</td>"+
"<td><button class='show-modal btn btn-info btn-sm' data-id='" + data.id + "' data-bank_id='" + data.bank_name + "' data-bankbranch_id='" + data.bankbranch_location + "' data-name='" + data.name + "' data-phone='" + data.phone + "'><span class='fa fa-eye'></span></button> <button class='edit-modal btn btn-warning btn-sm' data-id='" + data.id + "' data-bank_id='" + data.bank_name + "' data-bankbranch_id='" + data.bankbranch_location + "' data-name='" + data.name + "' data-phone='" + data.phone + "'><span class='glyphicon glyphicon-pencil'></span></button> <button class='delete-modal btn btn-danger btn-sm' data-id='" + data.id + "' data-bank_id='" + data.bank_name + "' data-bankbranch_id='" + data.bankbranch_location + "' data-name='" + data.name + "' data-phone='" + data.phone + "'><span class='glyphicon glyphicon-trash'></span></button></td>"+
"</tr>");
}
});
$('#name').val('');
$('#phone').val('');
})