sqlstate[hy000]:常规错误:1364字段“uid”没有默认值

cnwbcb6i  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(369)

从拉威尔开始。我已经附加了我的用户和配置文件模型以及配置文件控制器。我的目标是在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');
        });
    }
}

配置文件控制器文件

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');
    }
}
uubf1zoe

uubf1zoe1#

更改迁移文件,因为您希望稍后定义您的关系,所以您的外部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');
    }
}
kyvafyod

kyvafyod2#

如果在程序中不提供值,则需要在表定义级别提供默认值。
根据您的描述,在创建用户记录之后,似乎缺少创建概要文件的功能。

相关问题