【发布时间】:2018-04-17 14:19:50
【问题描述】:
我正在尝试从用户表中获取(然后显示)带有外键的文章表中的创建者/作者姓名。我只是 laravel 的新手,希望你能帮我解决这个问题。我对 int 类型的 F-key(s) 没有任何问题,但对于字符串类型,我可能在某处遗漏了一些东西。有时它会给我一些错误,有时一切正常,但文章表上的 user_name 只是保持为空。 如果您需要有关某事的更多信息,请发表评论。 提前致谢!
文章的架构
Schema::create('articles', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('user_name')->nullable();
$table->string('title');
$table->text('body');
$table->timestamps();
$table->timestamp('published_at');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('user_name')
->references('name')
->on('users')
->onDelete('cascade');
});
用户的架构
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
【问题讨论】:
-
使用 eloquent 是另一种方式,很简单
标签: php string laravel null key