简体   繁体   English

Laravel 5.0试图保存文件并将文件路径存储到数据库

[英]Laravel 5.0 trying to save file and store filepath to db

I am trying to save an image and store it's location to the database but for some reason I always get null when I dump the request from my controller. 我正在尝试保存图像并将其位置存储到数据库中,但是由于某些原因,当我从控制器中转储请求时,我总是得到null。 Not sure what I am doing wrong. 不知道我在做什么错。

My form: 我的表格:

<form class="addorUpdateBlockItem" method="post" enctype="multipart/form-data">
    <div class="input-group item">
        <label for="content">label</label>
        <input type="text" hidden name="item_type" value="image">
        <input type="file" name="content" id="contentImageBlockId3" class="form-control">
        <span class="input-group-btn">
            <button type="submit" class="btn btn-default plus margin-top">opslaan</button>
        </span>
    </div>
</form>

Now in my controller I try to access the image in my request like this: 现在在我的控制器中,我尝试像这样访问请求中的图像:

dd($request->input('content'));

This results in null 结果为空

My controller method where I do the dump 我执行转储的控制器方法

/**
 * Add or Update a BlockItemContent to a BlockNewsletter item.
 *
 * @param $blockNewsletterPivotId
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function addOrUpdateBlockItemContent($blockNewsletterPivotId, Request $request)
{
    if ($request->input('item_type') == "image") {
        dd($request->hasFile('content'));
    }
}

this results in false 这导致错误

EDIT 编辑

I forgot everything happens through an ajax call maybe it has something to do with it: 我忘记了所有通过ajax调用发生的事情,也许与它有关:

$(document).on('submit', '.addorUpdateBlockItem', function (e) {
        e.preventDefault();

        // Retrieve form data
        let formData = new FormData();

        let data = $(this).serializeArray();
        $.each(data, function(index, field) {
            formData.set(field.name, field.value);
        });

        $.ajax({
            url: $(this).attr('action'),
            data: formData,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function(data){
                if (data.data.actionType === 'update') {
                    $('.successMessage').html(data.data.item + ' succesvol bijgewerkt.');
                }

                if (data.data.actionType === 'insert') {
                    $('.successMessage').html(data.data.item + ' succesvol toegevoegd.');
                }

                $('.successMessage').show();
            },
            error: function(error) {
                console.log(error);
            }
        });
    });

Replace your formdata section here: 在此处替换formdata部分:

let formData = new FormData();

let data = $(this).serializeArray();
$.each(data, function(index, field) {
    formData.set(field.name, field.value);
});

With this: 有了这个:

let formData = new FormData($(this)[0]);

You can access the file with $request->file('content') . 您可以使用$request->file('content')访问文件。

Check also $request->hasFile('content') , it should return you true . 还检查$request->hasFile('content') ,它应该返回true

you did this line : 你做这行:

$.ajax({
        url: $(this).attr('action'),..

but in your form you did not specified any action: 但是在您的表单中,您未指定任何操作:

<form class="addorUpdateBlockItem" method="post" enctype="multipart/form-data">

so try to do this and let me see your dd result: 因此,尝试执行此操作,让我看看您的dd结果:

<form class="addorUpdateBlockItem" action="{{ route('your_upload_route') }}" method="post" enctype="multipart/form-data">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM