【发布时间】:2020-03-06 13:47:36
【问题描述】:
如果我有两个相关的表,我不明白如何将信息返回给blade template:
第一个表是标准的Laravel 'users' 表
第二张表:
Schema::create('recipes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('code', 10);
$table->string('description');
$table->float('size');
$table->bigInteger('created_by')->unsigned();
$table->string('status')->default('pending');
$table->boolean('deleted');
$table->timestamps();
$table->foreign('created_by')
->references('id')
->on('users')
->onDelete('cascade');
}
比我有两个Controllers:User 和Recipe
Recipe有
public function user()
{
return $this->belongsTo(\App\User::class);
}
和User 有
public function recipes()
{
return $this->hasMany(\App\Recipe::class);
}
实际输出如下所示 (RecipesController):
$recipes = Recipe::latest()->paginate($perPage);
return view('admin.recipes.index', compact('recipes'));
一切看起来都不错,但列 created_by 包含 users 主键女巫是整数。如何显示用户名?这有点像内部连接,但有可能以雄辩的方式做到这一点吗?还是我完全误解了Model 中的那些公共功能?
【问题讨论】:
标签: php laravel symfony eloquent