Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩地从相关表格中获取详细信息_Php_Laravel_Symfony_Eloquent - Fatal编程技术网

Php Laravel雄辩地从相关表格中获取详细信息

Php Laravel雄辩地从相关表格中获取详细信息,php,laravel,symfony,eloquent,Php,Laravel,Symfony,Eloquent,如果我有两个相关的表,我不知道如何将信息返回到bladetemplate: 第一个表是标准的“用户”表 第二张表: Schema::create('recipes', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('code', 10); $table->string('description'); $table->float('size'

如果我有两个相关的表,我不知道如何将信息返回到
blade
template

第一个表是标准的“用户”表 第二张表:

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');
}
我有两个
控制器
用户
配方
Recipe
have

public function user()
{
    return $this->belongsTo(\App\User::class);
}
用户
拥有

public function recipes()
{
    return $this->hasMany(\App\Recipe::class);
}
实际输出如下所示(
RecipesController
):


所有内容看起来都正常,但由contain
users创建的列
主键为整数。如何显示用户名?这有点像内部连接,但有可能做到这一点,在雄辩?或者我完全误解了
模型中的公共功能

配方模型中的用户关系缺少外键:

public function user()
{
    return $this->belongsTo(\App\User::class, 'created_by');
}
然后,您可以在控制器中向用户加载您的食谱:

$recipes = Recipe::with('user')->latest()->paginate($perPage);
return view('admin.recipes.index', compact('recipes'));
最后,您可以访问视图中的用户:

@foreach($recipes as $recipe)
    {{ $recipe->user->name }}
@endforeach

您可以在中阅读更多关于一对多关系的反向信息。

配方模型中的用户关系缺少外键:

public function user()
{
    return $this->belongsTo(\App\User::class, 'created_by');
}
然后,您可以在控制器中向用户加载您的食谱:

$recipes = Recipe::with('user')->latest()->paginate($perPage);
return view('admin.recipes.index', compact('recipes'));
最后,您可以访问视图中的用户:

@foreach($recipes as $recipe)
    {{ $recipe->user->name }}
@endforeach

您可以在中阅读有关一对多关系倒数的更多信息。

我有错误:找不到类“App\User”。我不理解witch模型中的错误:找不到类“App\User”。我不懂女巫模型