SQLSTATE [HY000]:常规错误:1364字段'user_id'没有默认值

时间:2017-06-08 15:58:11

标签: laravel laravel-5.3

我已通过以下迁移在我的数据库中创建了一个新列:

public function up(){
  Schema::table('complains', function (Blueprint $table) {
  $table->integer('user_id')->after('id');
   });
}

当我填写表单将数据发布到数据库中时,我收到以下错误:

SQLSTATE [HY000]:常规错误:1364字段'user_id'没有默认值(SQL:插入complainstitlebody,{{1} },nameregnumberupdated_at)值(测试用户ID,测试用户ID,John,cs-282-2145 / 2010,2017-06-08 18:47:53 ,2017-06-08 18:47:53))

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

我今天在用户注册时遇到了类似的问题,而且我得到了一个

SQLSTATE [HY000]:常规错误:1364字段'密码'没有默认值(SQL:插入users

我通过在我的受保护的$ fillable数组中添加密码来修复它并且它可以正常工作

protected $fillable = [ 'name', 'email', 'password', ]; 我希望这会有所帮助。

答案 1 :(得分:1)

可能您没有为user_id

发送值

其他可能性,你可以"转换"列user_id 可以为空的

答案 2 :(得分:1)

以下是我的控制器的代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Complain;
use Illuminate\Support\Facades\Redirect;
use Session;
use Auth;

class ComplainController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('home');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, array(

            'title' => 'required|max:255',
            'body' => 'required'

            ));

        $complain = new Complain;

        $complain->user_id = Auth::user()->id;

        $complain->title = $request->title;
        $complain->body = $request->body;

        $complain->save();

        Session::flash('success', 'Your complain was sent to the operator, please wait for feedback.');

        return redirect::back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $complain = Complain::find($id);
        return view('admin')->withPost($complain);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

这是我的模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Complain extends Model
{

}