Home > Back-end >  Upload multiple forms no save in mysql database laravel 9
Upload multiple forms no save in mysql database laravel 9

Time:05-03

could someone help me to find the error? only the post_image input is saved the others are not saved in the database what am I doing wrong? I've checked it several times but only the filename of the post_image field is saved in the database, the other files are not persisting in the database, could someone help me where I'm going wrong? Every help is welcome. Thank you very much in advance.

create.blade.php

div >
                    <label for="post_image" >{{ __('Image') }}</label>

                    <div >
                        <input type="file" name="post_image"/>

                        @error('post_image')
                        <span  role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                        @enderror
                    </div>
                    <br>
                    <hr>

                    <label for="post_video" >{{ __('Video') }}</label>
                    <div >
                        <input type="file" name="post_video"/>

                        @error('post_video')
                        <span  role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                        @enderror
                    </div>

                    <br>
                    <hr>

                    <label for="post_gif" >{{ __('GIF') }}</label>
                    <div >
                        <input type="file" name="post_gif"/>

                        @error('post_gif')
                        <span  role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                        @enderror
                    </div>

                    <br>
                    <hr>
                </div>

function store in controller -->

public function store(Request $request, Community $community)
{
    $user = Auth::user();

    $creds = $request->validate([

        'post_image' => '|image|mimes:jpeg,jpg,png,svg|max:18048',
        'post_video' => 'mimes:mp4,ogx,oga,ogv,ogg,webm|max:180048',
        'post_gif' => '|image|mimes:gif|max:18048',


        'title' =>  ['required'],
        'post_text' =>  ['required'],
        'post_url' =>  ['required']


    ]);

    //IMAGE JPG,PNG,SVG
    if ($image = $request->file('post_image')) {
        $destinationPath = 'media/uploads';
        $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
        $image->move($destinationPath, $profileImage);
        $input['post_image'] = "$profileImage";
    }
    //END IMAGE JPG,PNG,SVG

    //VIDEO MP4

    if ($image = $request->file('post_video')) {
        $destinationPath = 'media/uploads';
        $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
        $image->move($destinationPath, $profileImage);
        $inputmp4 ['post_video'] = "$profileImage";
    }

    //END VIDEOS

    // GIF IMAGES

    if ($image = $request->file('post_gif')) {
        $destinationPath = 'media/uploads';
        $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
        $image->move($destinationPath, $profileImage);
        $inputgif ['post_gif'] = "$profileImage";
    }

    //END GIF IMAGES



    $post = $community->posts()->create
        (['user_id' => auth()->id(),
        'title' => $creds['title'],
        'post_text' => $creds['post_text'],
        'post_url' => $creds['post_url'],
        'post_image' => $input['post_image'] ?? '',
        'post_video' => $inputmp4['post_video'] ?? '',
        'post_gif' => $inputgif['post_gif'] ?? '',





    ]);

    return redirect()->route('communities.show', $community);

}

CodePudding user response:

Is it possible post_video and post_gif are empty? Php has a max_upload and max post size limit that is defined on the ini file. It is possible that that size is being exceeded. Hence your files are not actually uploading.

Also, your if statements are kinda confusing. Check this out :

    //This if statement will always return true. Since you're assigning $image variable to $request->file('post_video').
    if ($image = $request->file('post_video')) {
        //Code For saving file
        //Don't use this
    }

    // Instead you should use hasFile to check if the file is uploaded. here's how
    if ($request->hasFile('post_video')) {
        $video = $request->file('post_video');
        //Code For saving file
        //Use This instead
    }

Also, at the last line, where you call create() method, do this:

    $post = $community->posts()->create
        (['user_id' => auth()->id(),
        'title' => $creds['title'],
        'post_text' => $creds['post_text'],
        'post_url' => $creds['post_url'],
        'post_image' => $input['post_image'], // skip the "?? ''" part
        'post_video' => $inputmp4['post_video'],
        'post_gif' => $inputgif['post_gif'],
    ]);

Now, if either post_image, post_video or post_gif is empty, you'll get a error and can see if the data is wrong.

Also, on a kinda related note, did you know laravel has a good helper to save files in storage? Check it out

CodePudding user response:

Are you sure you are getting the image on post? I can't see your whole code for the form. Are you using enctype="multipart/form-data" when you post?

  • Related