skip to Main Content

i have a form and i want to send it to laravel, and it does have files.
I’ve tried a lot of ways but i was not able to upload photo.
When i try to get file it says null.

dd($request->file('file'));

here is my js section:

    

function newUser() {
        let formData = new FormData();
        formData.append('category_id', $("#parentCombo").val());
        formData.append('sub_category_id', $("#childrenCombo").val());
        formData.append('province_id', $("#provinceCombo").val());
        formData.append('city_id', $("#cityCombo").val());
        formData.append('area', $("#area").val());
        formData.append('price', $("#price").val());
        formData.append('phone', $("#phone").val());
        formData.append('age', $("#age").val());
        formData.append('status', $("#status").val());
        formData.append('email', $("#email").val());
        formData.append('file', $('#file'));
        formData.append('description', $("#description").val());
        formData.append('title', $("#title").val());
        formData.append('room', $("#room").val());


        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.post({

            url: '{{ url('admin/notices') }}',
            data: formData,
            processData: false,
            contentType: false,
            async: true,
            success(data) {
                if (data['status'] == 201) {
                    swal({
                        title: 'با موفقیت انجام شد!',
                        text: data['message'],
                        type: 'success',
                        padding: '2em'
                    })
                }
            },
            error(err) {
                swal({
                    title: 'عملیات شکست خورد!',
                    text: err.statusText,
                    type: 'error',
                    padding: '2em'
                })
                console.log(err)
            }

        })
    }
</script>

and here is my Laravel Controller:

        $notice->picture = $request->file('file') ? $this->storeImage() : null;
        $notice->title = $request->get('title');
        $notice->description = $request->get('description');
        $notice->area = $request->get('area');
        $notice->room = $request->get('room');
        $notice->price = $request->get('price');
        $notice->category_id = $request->get('category_id');
        $notice->sub_category_id = $request->get('sub_category_id');
        $notice->province_id = $request->get('province_id');
        $notice->city_id = $request->get('city_id');
        $notice->phone = $request->get('phone') ?? null;
        $notice->status = $request->get('status');
        $notice->age = $request->get('age');
        $notice->user_ip = ip2long($request->ip());
        $notice->user_agent = $request->userAgent();
        $notice->region = $request->get('region');
        $notice->user_id = auth()->user()->id ?? null;
        $res = $notice->save();
        if($res){
            return response([
                'status' => 201,
                'message' => 'عملیات با موفقیت انجام شد'
            ]);
        }

NOTE: IT DOES WORK WITH USUAL HTTP REQUESTS. I DO NOT KNOW HOW CAN I SEND FILE WITH JQUERY THAT I CAN GET IT IN LARAVEL WITH $request->file('file');

2

Answers


  1. Can you add

    enctype="multipart/form-data"
    

    In form, because it cause issue while sending media to controllers.

    Login or Signup to reply.
  2. You need to append your file as below.

    formData.append('file', $('#file')[0].files[0]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search