Check your javascript code, you don't pass the file when you call the store method
Jun 18, 2022
17
Level 2
call to a member function extension() on null
hello , i want to upload image with dropzone . but i get this error
<script type="text/javascript">
Dropzone.options.fileDropzone = {
maxFileSize : 12 ,
addRemoveLinks : true,
acceptedFiles : ".jpg , .jpeg , .png , .svg",
url : "{{route('posts.store')}}",
headers:{
'X-CSRF-TOKEN': "{{csrf_token()}}"
},
removeFile: function(file){
let name = file.upload.image;
$.ajax({
type:'post',
url : "{{route('remove.image')}}",
data : {
"_token": "{{csrf_token()}}" ,
name: name
},
success:function(data){
console.log("uploaded successfully");
},
error:function(e){
console.log(e);
},
})
let fileRef;
return (fileRef = file.previewElement) != null ?
fileRef.parentNode.removeChild(file.previewElement) : void 0;
},
success:function(file , response){
console.log(file);
}
}
</script>
<form action="{{route('posts.store')}}" method="post" enctype="multipart/form-data" >
@csrf
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="{{old('title')}}" placeholder="title">
</div>
<div class="mb-3">
<label for="image" class="form-label">Image</label>
<div class="dropzone" id="file-dropzone" >
</div>
</div>
<div class="mb-3">
<button class="btn btn-info">Created</button>
<a href="{{route('posts.index')}}" class="btn btn-danger">Cancel</a>
</div>
</form>
public function store(Request $request)
{
$image = Carbon::now()->microsecond . '.' . $request->file('file')->extension();
$request->file('file')->storeAs('image' , $image , 'public_drive');
Post::query()->create([
'title' => $request->title,
'image' => $image,
]);
return response()->json(['success' => $image]);
}
Please or to participate in this conversation.