php - SQLSTATE[HY000] : General error: 1364 Field 'uID' doesn't have a default value

标签 php mysql database laravel xampp

刚刚开始使用 Laravel。我已将我的用户和配置文件模型与配置文件 Controller 一起附加。我的目标是自动在 profile 表中分配外键 uID。任何帮助将不胜感激。

用户模型文件

namespace App;

use Illuminate\Database\Eloquent\Model;

class user extends Model
{
    // specify which attributes can be filled out during registration
    public $timestamps = false;
    protected $fillable=['firstname','lastname','email','password',];

    public function profile(){
      return $this->hasOne(profile::class,'pID','uID');
    }
}

配置文件模型文件

namespace App;

use Illuminate\Database\Eloquent\Model;

class profile extends Model
{
    //
    public $timestamps = false;
    protected $fillable = ['summary','uID'];

    public function user(){
      return $this->belongsTo(user::class,'uID','pID');
    }
}

配置文件迁移文件

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
    public function up()
    {
      // create profile table
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('pID');
            $table->timestamp('created_at')->useCurrent();
            $table->string('summary')->default('');
            $table->integer('uID')->unsigned();

            $table->foreign('uID')->references('uID')->on('users')->onDelete('cascade');
        });
    }
}

配置文件 Controller 文件

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\profile;

class ProfileController extends Controller
{
    public function store(Request $request)
    {
       // used to store user profile after validation
        $this->validate($request,[
          'summary' => 'required'
        ]);
        $profile = new profile([
          'summary' => $request->get('summary'),
        ]);
        $profile->save();

        return redirect()->route('profile.create')->with('success','Profile created');
    }
}

最佳答案

更改您的迁移文件, 由于您想稍后定义您的关系,因此您的外部 id 字段应该可以为空。

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
public function up()
{
// create profile table
Schema::create('profiles', function (Blueprint $table) {
$table->increments('pID');
$table->timestamp('created_at')->useCurrent();
$table->string('summary')->default('');
$table->integer('uID')->nullable()->unsigned();

$table->foreign('uID')
        ->references('uID')
        ->on('users')
        ->onDelete('cascade');
});
}
}

如果您想在创建配置文件后分配登录用户,

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\profile;

class ProfileController extends Controller
{
    public function store(Request $request)
    {
       // used to store user profile after validation
        $this->validate($request,[
          'summary' => 'required'
        ]);
        $profile = new profile([
          'summary' => $request->get('summary'),
          'uID'     => auth()->user()->id,

        ]);
        $profile->save();

        return redirect()->route('profile.create')->with('success','Profile created');
    }
}

关于php - SQLSTATE[HY000] : General error: 1364 Field 'uID' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678509/

相关文章:

php - 将链接转换成图片

java - 如何使用 HQL(Hibernate 查询语言)获取列的最后一个值

swift - Realm :无法使用 'add(_:update:)' 类型的参数列表调用 '(String)'

php - 我想使用重写规则来清除我的网址

php - 我可以在准备好的语句中参数化表名吗?

c# - 与数据库的父子进程通信的最佳设计

asp.net - 什么时候应该使用 session-per-request 模式

php - 逗号分隔具有一个值的步骤到具有相同值的多个步骤

javascript - 只想在用户单击 html 表单上的提交时显示绿色复选标记

php - 为什么 get_object_vars 返回 protected 属性?