首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在mysql数据库laravel 9中上载多个表单

在mysql数据库laravel 9中上载多个表单
EN

Stack Overflow用户
提问于 2022-05-02 18:10:47
回答 1查看 90关注 0票数 0

有人能帮我找出错误吗?只有post_image输入被保存,其他的没有保存在数据库中,我做错了什么?我已经检查过几次了,但是只有post_image字段的文件名保存在数据库中,其他文件没有保存在数据库中,有人能帮我找出错误的地方吗?一切帮助都是受欢迎的。先谢谢你。

create.blade.php

代码语言:javascript
复制
div class="form-group row">
                    <label for="post_image" class="col-md-4 col-form-label text-md-right">{{ __('Image') }}</label>

                    <div class="col-md-6">
                        <input type="file" name="post_image"/>

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

                    <label for="post_video" class="col-md-4 col-form-label text-md-right">{{ __('Video') }}</label>
                    <div class="col-md-6">
                        <input type="file" name="post_video"/>

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

                    <br>
                    <hr>

                    <label for="post_gif" class="col-md-4 col-form-label text-md-right">{{ __('GIF') }}</label>
                    <div class="col-md-6">
                        <input type="file" name="post_gif"/>

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

                    <br>
                    <hr>
                </div>

控制器中的函数存储->

代码语言:javascript
复制
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);

}
EN

回答 1

Stack Overflow用户

发布于 2022-05-02 18:51:36

post_videopost_gif可能是空的吗?Php有一个在ini文件上定义的上传和最大post大小限制。有可能超出了这一规模。因此,您的文件实际上并没有上传。

还有,你的if语句有点混乱。看看这个:

代码语言:javascript
复制
    //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
    }

另外,在最后一行,即调用create()方法时,执行以下操作:

代码语言:javascript
复制
    $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'],
    ]);

现在,如果post_image、post_video或post_gif都是空的,您将得到一个错误,并可以查看数据是否错误。

另外,您知道laravel有一个很好的助手可以在存储中保存文件吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72090676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档