Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Retrieving all 'id' from a relationship

Hello,

Impossible for me to find the solution. I have tried very different things, but no result.

I have two three tables : drawings, techniques and drawing_technique. I have defined the relationships (direct and inverse). In the drawing view, the techniques are checkboxes.

  • I save the datas in the relationship table with sync() : no problem.
  • I want to edit the drawing, the checkboxes are not checked as it should be !

Here is my code.

In the controller.

public function edit(Drawing $drawing)
{
    $substrates = Substrate::orderBy('title', 'asc')->pluck('title', 'id');
    $techniques = Technique::orderBy('title', 'asc')->get();
    $categories = Category::orderBy('title', 'asc')->get();
    $checked_techniques = Technique::with('drawings')->find($drawing->id)->toArray();
    return view('drawings.edit', compact('drawing', 'substrates', 'techniques', 'categories', 'checked_techniques'));
}

The view.

<div class="form-group form-check form-check-inline">
    @foreach ($techniques as $technique)
        <div class="form-group custom-control custom-control-inline custom-checkbox">
            {{ Form::checkbox('techniques', $technique->id, in_array($technique->id, $checked_techniques) ? true : false, ['class' => 'custom-control-input', 'id' => 'technique-'.$technique->id, 'name' => 'techniques[]']) }}
            {{ Form::label('technique-'.$technique->id, $technique->title, ['class' => 'custom-control-label']) }}
        </div>
    @endforeach
</div>

If somebody could help me, thank you ;).

0 likes
1 reply
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

I have found the solution.

In my controller, I have to use this code :

$checked_techniques = $drawing->techniques()->wherePivot('drawing_id', $drawing->id)->pluck('id')->toArray();

Please or to participate in this conversation.