首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我使用的是laravel 5.8,无法向mysql数据库插入数据

我使用的是laravel 5.8,无法向mysql数据库插入数据
EN

Stack Overflow用户
提问于 2019-05-20 17:50:19
回答 3查看 2.1K关注 0票数 1

我正在使用laravel 5.8并学习CRUD。我有2个表,用户表和地址表。我想将user_id连接到

地址表。但是我的代码不能工作。我无法插入数据。

我已经迁移了2个表,并且我能够添加列。我插入了一个用户的数据。所以表中只有一个用户信息。

我在本地服务器上使用MAMP和mysql服务器。

1.App/Address.php

protected $fillable = [
     'name'
];//

2.App/User.php

public function address(){
    return $this->hasOne('App\Address', 'foreign_key');
}

3.Userstable

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.web.php/web.php

use 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);
});

日志文件上什么也没有显示,所以我不知道为什么它不能工作。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56218365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档