0

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?

4
  • released_items or released_item? Commented Mar 16, 2020 at 13:55
  • @TsaiKoga released_item Commented Mar 16, 2020 at 14:30
  • but fk_released_items_id is the column of recipe_lines. not recipe. And from the tables structure. It look like many-to-many relationship.And if line has-one released_item, then one recipe has-many released_items. Commented Mar 16, 2020 at 15:07
  • @TsaiKoga this is how it looks like. I have table released_items that's why fk_released_items_id. First join one to many one recipe to multiple lines and from second table I need to join line with released_item (table name: released_items) Commented Mar 16, 2020 at 15:13

1 Answer 1

1

Your relationships is not setup correctly in your model. I understand a Recipe has one or more Recipe Line, but it is not clear if a Recipe has one Release Item OR if a Recipe Line has one Release Item.

Based on your comment below, the relationship below should be in your Recipe Line model, not the Recipe model:

public function released_item()
{
   return $this->hasOne(\App\ReleasedItem::class, 'fk_released_items_id');
}

Then your query should be something like below:

public function show($id)
{
    $lines = Recipe::with('lines.released_item)->findOrFail($id);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Correct Recipe can have one or more lines and from lines i need to create another join to released items where one line have one released item

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.