首页 > 解决方案 > Laravel 问题请求图像数据也将其存储在其他地方

问题描述

我遇到了一个我现在无法自行解决的问题 2 天。

我正在使用 laravel 并创建一个帖子:

在那里,我正在创建帖子,并且还想在浏览器的视图层中选择要选择的照片。它应该被选中并存储在我的帖子数据库中。您会看到我的 PostController 的代码。

public function store(Request $request)
    {
        // Validate posted form data
        $validated = $request->validate([
            'title' => 'required|string|unique:posts|min:5|max:100',
            'content' => 'required|string|min:5|max:2000',
            'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:5000',
        ]);

         $data['title'] = $request->title;
        $data['content'] = $request->content;
        $data['image_gallery'] = $request->path;


        // Create and save post with validated data
        $post = Post::create($validated);


        // Redirect the user to the created post with a success notification
        return redirect(route('posts.index'))->with('notification', 'Post erstellt');
    }

还有我的帖子创建视图的代码:

    <label class="label">Beitragsbild: </label>
    <div class="field">
            <select id="id_select2_example" style="width: 220px;">
                    @foreach($images as $image)
                        <option data-img_src="/image/{{ "$image->id" }}">{{ $image->title }}</option>
                    @endforeach
            </select>
    </div>

这是来自浏览器的错误,但我无法利用它。

SQLSTATE[HY000]:一般错误:1364 字段“image_gallery_id”没有默认值(SQL:插入poststitle、、、、content) 值updated_atcreated_at

如果我能得到一些提示/进一步的帮助,我将不胜感激!

标签: phplaravel

解决方案


推荐阅读