'标题'在Laravel中没有默认值错误500

时间:2018-01-30 12:54:45

标签: php ajax laravel

您好我正在尝试使用表单数据创建帖子,因为我有一个文件输入来上传图像。我真的很难让上传文件工作。当我尝试使用消息

创建帖子时收到错误500
  

SQLSTATE [HY000]:常规错误:1364字段'标题'没有默认值(SQL:插入posts(' updated_at',' created_at')值(2018-01-30 12:31:18 ,2018-01-30 12:31:18)。

我不确定默认值是什么意思。我检查了$fillables,标题肯定在那里。当我在控制器的顶部运行以下代码时,它会通过:

$request->all();
return response()->json('passed');

但是当我在控制器中有以下内容时会出现错误:

public function create(Request $request)
{
    Post::create($request->all());

    $response = [
        'response' => 'Post Created Successfully',
        'error' => 'Something went wrong'
    ];

    return response()->json($response);
}

这是我的HTML:

<div class="container">
    <div class="row">
        <h1>Create your post</h1>
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" name="title" id="title" class="form-control">
        </div>
        <div class="form-group">
            <label for="post">Post</label>
            <textarea name="post" rows="8" cols="80" id="post" class="form-control"></textarea>
        </div>
        <div class="form-group">
            <label for="image">Add image</label>
            <input type="file" name="image" id="image" class="form-control">
        </div>
        <input type="submit" name="submit" value="Submit Post" id="submit" class="btn btn-primary">
    </div>
</div>

AJAX:

<script>
$(document).ready(function() {
    $("#submit").on("click", function(e) {
        e.preventDefault();

        var formData = new FormData();
        var fileData = $('#image').prop('files')[0];
        var title = $('#title').val();
        var post = $('#post').val();

        formData.append('fileData', fileData);
        formData.append('title', title);
        formData.append('post', post);

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

        $.ajax({
            url:'/post/create/create',
            type: "POST",
            data: {
                formData: formData
            },
            processData: false,
            contentType: false,
            dataType: 'json',

            success:function(response){
                toastr.success(response.response);
            },
            error: function(error){
                toastr.error(error.error)
            }
        });

    });
});
</script>

这是我的Post模型与fillables:

protected $fillable = ['title', 'post', 'user_id', 'uploadImage'];

1 个答案:

答案 0 :(得分:0)

为了从控制器中的ajax获取数据,请将数据作为

传递
data: formData

而不是

data: {
     formData: formData
}

您可以将控制器中的信息处理为

public function create(Request $request)
{

    $path = '';

    if ($request->hasFile('fileData')) {
        if ($request->file('fileData')->isValid()) {
            // Upload the file
            $path = $request->fileData->store('your directory');
        }
    }

    $post = Post::create([
        'title' => $request->title,
        'post' => $request->post,
        'user_id' => auth()->user()->id,
        'uploadImage' => $path
    ]);

    if ($post) {
        return response()->json([
            'response' => 'Post Created Successfully!'
        ]);
    }

    return response()->json([
        'error' => 'Something went wrong!'
    ], 500)

}

Documentation关于存储上传的文件