Laravel eloquent get details from related table

I don’t understand how to return info back to blade template if I have two related tables:

First table is standard Laravel ‘users’ table
Second table:

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

Than I have two Controllers: User and Recipe
Recipe have

public function user()
{
    return $this->belongsTo(AppUser::class);
}

and User have

public function recipes()
{
    return $this->hasMany(AppRecipe::class);
}

actual output looks like this (RecipesController):

$recipes = Recipe::latest()->paginate($perPage);
return view('admin.recipes.index', compact('recipes'));

everything looks OK but column created_by contain users primary key witch is integer. How can I display users name? This is something like inner join but is it possible to do that in eloquent? Or I completely misunderstanding those public functions in a Model?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *