如何将功能正常的图片上传按钮添加到现有表单?

我正在尝试将图像上传到预先存在的表单,但是在提交表单时出现错误 SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value。


我已经设法让表单上的实际按钮和文件选择器起作用,但是,在提交时它会引用上述错误。关于这个问题的任何帮助或资源都会很棒。


后控制器


<?php


namespace App\Http\Controllers;


use App\Post;

use Illuminate\Http\Request;


class PostController extends Controller

{

    public function __construct()

    {

        $this->middleware('auth');

    }


    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function index()

    {

        $posts = Post::all();


        return view('post.index', compact('posts'));

    }


    /**

     * Show the form for creating a new resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function create()

    {

        $post = new Post();


        return view('post.create', compact ('post'));

    }


    /**

     * Store a newly created resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @return \Illuminate\Http\Response

     */

    public function store(Request $request)

    {

        $data = request()->validate([

            'title' => 'required',

            'body' => 'required',

        ]);


        $post = \App\Post::create($data);


        return redirect('/posts');

    }


    /**

     * Display the specified resource.

     *

     * @param  \App\Post  $post

     * @return \Illuminate\Http\Response

     */

    public function show(Post $post)

    {

        return view('post.show', compact('post'));

    }


    /**

     * Show the form for editing the specified resource.

     *

     * @param  \App\Post  $post

     * @return \Illuminate\Http\Response

     */

    public function edit(Post $post)

    {

        return view('post.edit', compact('post'));

    }


    /**

     * Update the specified resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @param  \App\Post  $post

     * @return \Illuminate\Http\Response

     */


森栏
浏览 110回答 1
1回答

MYYA

我认为默认情况下 user_id 没有默认值。在您的帖子模型中,protected&nbsp;$fillable&nbsp;=&nbsp;['user_id',&nbsp;'title',&nbsp;'body',&nbsp;'image'];同样在您的商店功能中,您必须包括来自当前登录用户的用户。&nbsp; &nbsp; use App\Post;&nbsp; &nbsp; use Auth;&nbsp; &nbsp; public function store(Request $request)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'body' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post = new Post();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post->user_id = Auth::user()->id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post->title = $request->title;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post->body = $request->body;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect('/posts');&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP