我正在使用 laravel 5.8 并學習 CRUD。我有 2 個表,用戶表和地址表。我想將 user_id 連接到地址表。但是我的代碼不起作用。我無法插入數據。我已經遷移了 2 個表,并且可以添加列。我插入了一位用戶的數據。所以表有一個用戶的信息。我在本地服務器中使用 MAMP 和 mysql 服務器。1.應用程序/地址.phpprotected $fillable = [ 'name'];//2.應用程序/用戶.phppublic function address(){ return $this->hasOne('App\Address', 'foreign_key');}3.用戶穩定Schema::create('users', function (Blueprint $table) { $table->Increments('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });4.地址表Schema::create('addresses', function (Blueprint $table) { $table->Increments('id'); $table->integer('user_id')->unsigned()->nullable(); $table->string('name'); //$table->timestamps(); });5.routes/web.phpuse App\User;use App\Address;Route::get('/', function () {return view('welcome');});Route::get('/insert', function(){$user = User::findOrFail(1);$address = new Addresses(['name'=>'1234 Houston av NY NY 234']);$user->address()->save($address);});日志文件上沒有顯示任何內容,所以我無法弄清楚為什么它不起作用。
我使用的是 Laravel 5.8,我無法將數據插入到 mysql 數據庫中
慕婉清6462132
2021-06-14 16:50:29