I have never came across this issue before while working on Laravel.
I have a form that will insert the product details which also has an image field.
Here's how I am creating the view for the insertion of product details:
{!! Form::open(['url' => '/admin/products', 'autocomplete' => 'off', 'id' => 'formAddProduct', 'files' => true]) !!}
<div class="errors"></div>
@include('admin.products.form', ['submitButtonText' => 'Add Product', 'submitButtonId' => 'btnAddProduct'])
{!! Form::close() !!}
form.blade.php:
<div class="form-group">
{!! Form::label('code', 'Code') !!}
{!! Form::text('code', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('short_description', 'Short Description:') !!}
{!! Form::text('short_description', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Long Description:') !!}
{!! Form::text('description', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('price', 'Price:') !!}
{!! Form::text('price', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('discount_price', 'Discount Price:') !!}
{!! Form::text('discount_price', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('display', 'Display Status:') !!}
{!! Form::select('display', ['Enabled' => 'Enabled', 'Disabled' => 'Disabled'], null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('image_file', 'Image:') !!}
{!! Form::file('image_file', ['id' => 'image_file', 'class' => 'form-control input-sm']) !!}
</div>
<div class="form-group">
{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary btn-block', 'id' => $submitButtonId]) !!}
</div>
Controller method:
public function store( Request $request ) {
$this->validate($request, [
'code' => 'required|alpha_dash|unique:products',
'name' => 'required',
'category_id' => 'required|integer',
'short_description' => 'string',
'description' => 'required',
'price' => 'required|regex:/^\d*(\.\d{2})?$/',
'discount_price' => 'regex:/^\d*(\.\d{2})?$/',
'display' => 'required|in:Enabled,Disabled',
'image_file' => 'mimes:jpg'
]);
if ( $request->ajax() ) {
Product::create( $request->all() );
if ( $request->file( 'image_file' ) ) {
$filename = Safeurl::make( $request->get( 'code' ) );
$image = Image::make( $request->file( 'image_file' ) );
$path = public_path( 'images/uploads/products/' );
$image->resize( 450, 450 )->save( $path.$filename.'.jpg', 100 );
} else {
return response(['msg' => 'No File']);
}
return response(['status' => 'success', 'msg' => 'The product has been added successfully.']);
}
return response(['status' => 'failed', 'msg' => 'The product could not be added successfully.']);
}
And the ajax:
$('#btnAddProduct').on('click', function() {
var inputData = $('#formAddProduct').serialize();
$.ajax({
url: '{{ url('/admin/products') }}',
type: 'POST',
data: inputData,
success: function( message ) {
alert( message.msg );
if ( message.status === 'success' ) {
toastr.success('Product added successfully.');
toastr.options.closeButton = true;
toastr.options.showMethod = "slideDown";
toastr.options.hideMethod = "slideUp";
$('input').val( '' );
$('select').val( '' );
}
},
error: function( data ) {
if ( data.status === 422 ) {
var errors = data.responseJSON;
var errorsHtml = '<div class="alert alert-danger"><ul>';
errorsHtml += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>';
$.each( errors, function( key, value ) {
errorsHtml += '<li>' + value[0] + '</li>'; //showing only the first error.
});
errorsHtml += '</ul></div>';
$( '.errors' ).html( errorsHtml );
}
}
});
return false;
});
The error I get is No file
When I do var_dump( $request->file( 'image_file' ) );
I get null
in the Response Tab of the chrome
When I do var_dump( Input::file( 'image_file' ) );
I get null
in the Response Tab of the chrome
Where have I made mistake ? Kindly help me. Thanks.
P.S.: I have used Intervention
as my image uploading functionality.
You should add
data: formData,
andasync: false,
in your ajax callSo your ajax will be
Update :
Turn your
async:true
in your ajax callHave this in your Laravel Validator Rule
image_file' => 'mimes:jpeg,jpg,JPG'