I don't know how to create Laravel eloquent join when I have multiple tables:
Schema::create('released_items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('code', 10);
$table->string('description');
***
$table->timestamps();
});
Schema::create('recipes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('code', 10);
$table->string('description');
***
$table->timestamps();
});
public function up()
{
Schema::create('recipe_lines', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('fk_recipe_id')->unsigned();
$table->bigInteger('fk_released_items_id')->unsigned();
$table->string('code', 10);
$table->float('quantity');
$table->timestamps();
$table->foreign('fk_recipe_id')
->references('id')
->on('recipes')
->onDelete('cascade');
$table->foreign('fk_released_items_id')
->references('id')
->on('released_items')
->onDelete('cascade');
});
}
I have 'Recipe' model:
public function lines()
{
return $this->hasMany(\App\RecipeLine::class, 'fk_recipe_id');
}
public function released_item()
{
return $this->hasOne(\App\RecipeLine::class, 'fk_released_items_id');
}
And RecipeLines Controller:
public function show($id)
{
$lines = Recipe::with('lines', 'released_item')->where('recipes.id', $id)->findOrFail($id);
}
But I still can't extract a data from released_items. Anyone know hot it work and can explain?
released_itemsorreleased_item?fk_released_items_idis the column ofrecipe_lines. notrecipe. And from the tables structure. It look like many-to-many relationship.And iflinehas-onereleased_item, then onerecipehas-manyreleased_items.