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

发布于 2025-02-06 20:32:39 字数 3383 浏览 2 评论 0原文

SQLSTATE [HY000]:常规错误:1364字段'user_id'没有默认值(SQL:插入Reportsdetter> datery_name >,ic,<代码>电话,current_locationstatusemg_codeupdated_atcreate_at)值(DFD,女性,SDF,SDF,SDF,SDF,SDF,SDF,SDF,2022-06-13 05:53:11,2022-06-06-13 05:53 05:53 :11))

我可以知道如何解决这个问题吗? USER_ID是用户表的外键。

用户表:

  Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('profile_image');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');

            $table->rememberToken();
            $table->timestamps();
        });

用户模型:

public function reports()
{
    return $this->hasMany(Report::class);
}

报告表:

  Schema::create('reports', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('patient_name');
        $table->string('gender');
        $table->string('ic');
        $table->string('phone');
        $table->string('current_location');
        $table->string('status');
        $table->string('emg_code');
        $table->softDeletes();
        $table->timestamps();

       
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

    });

ReportController创建Func。:ReportController

  public function create()
    {
        $users = User::select('id', 'name')->get();
        $users = $users->pluck('name');

        return view('admin.reports.create', compact('users'));
    }

Store Func。:HASUSER

   public function store(Request $request)
    {
        $this->validate($request, [
            'patient_name' => 'required|string',
            'gender' => ['required', 'in:male,female'],
            'ic' => 'required|string',
            'phone' => 'required|string',
            'current_location' => 'required|string',
            'status' => 'required|string',
            'emg_code' => 'required|string'

        ]);
        $requestData = $request->all();

        $report = Report::create($requestData);


        foreach ($request->users as $user) {
            $report->assignUser($user);
        }

        return redirect('admin/reports')->with('flash_message', 'Report added!');
    }

特征:

public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function assignUser($user)
    {
        return $this->users()->save(
            User::whereName($user)->firstOrFail()
        );
}

报告模型:

class Report extends Model
{
    use HasUser;
    
          protected $fillable = [
                'user_id',
                'patient_name',
                'gender',
                'ic',
                'phone',
                'current_location',
                'status',
                'emg_code'
            ];

}

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into reports (patient_name, gender, ic, phone, current_location, status, emg_code, updated_at, created_at) values (dfd, female, sdf, sdf, sdf, sdf, sdf, 2022-06-13 05:53:11, 2022-06-13 05:53:11))

May I know how can I solve this? The user_id is the foreign key of the Users Table.

Users Table:

  Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('profile_image');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');

            $table->rememberToken();
            $table->timestamps();
        });

User Model:

public function reports()
{
    return $this->hasMany(Report::class);
}

Reports Table:

  Schema::create('reports', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('patient_name');
        $table->string('gender');
        $table->string('ic');
        $table->string('phone');
        $table->string('current_location');
        $table->string('status');
        $table->string('emg_code');
        $table->softDeletes();
        $table->timestamps();

       
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

    });

ReportController Create func.:

  public function create()
    {
        $users = User::select('id', 'name')->get();
        $users = $users->pluck('name');

        return view('admin.reports.create', compact('users'));
    }

ReportController Store func.:

   public function store(Request $request)
    {
        $this->validate($request, [
            'patient_name' => 'required|string',
            'gender' => ['required', 'in:male,female'],
            'ic' => 'required|string',
            'phone' => 'required|string',
            'current_location' => 'required|string',
            'status' => 'required|string',
            'emg_code' => 'required|string'

        ]);
        $requestData = $request->all();

        $report = Report::create($requestData);


        foreach ($request->users as $user) {
            $report->assignUser($user);
        }

        return redirect('admin/reports')->with('flash_message', 'Report added!');
    }

HasUser Trait:

public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function assignUser($user)
    {
        return $this->users()->save(
            User::whereName($user)->firstOrFail()
        );
}

Reports Model:

class Report extends Model
{
    use HasUser;
    
          protected $fillable = [
                'user_id',
                'patient_name',
                'gender',
                'ic',
                'phone',
                'current_location',
                'status',
                'emg_code'
            ];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文