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

cschoeni's avatar

List array from pivot table

How i can create create a array with user_id or other id from my pivot table (Many to Many relationship). The result must by $value = [1,2,5,7,8];

this is for my multiple checkboxes. With if function give the checkbox value true or false...

0 likes
3 replies
jekinney's avatar

Set your belongs to many relationship and you can eager load the connected model.

User::with('roles')->find($id);

This will load the pivot table and connected Role model.

In your view each checkbox input element check if

user->roles->contains(value)

Checked.

Arrays? It's oop, should be trying to work with objects in object oriented programming I think.

2 likes
cschoeni's avatar

in my Controller: $languages = Language::all(); $data = [1,2,3,4,5,6]; (And this work, now i test this with your answer)

in my Blade


@foreach ($languages as $language)

@endforeach
/.checkbox -->
cschoeni's avatar
cschoeni
OP
Best Answer
Level 4

/**
* The roles that belong to the user.
*/
public function languageList()
{
return $this->belongsToMany('App\Language')->withTimestamps()->lists('language_id');
}

With this works grade. But its not so ncie.

Please or to participate in this conversation.