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...
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.
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)
@if (in_array($language->id, $data))
{!! Form::checkbox('language[]', $language->id, true) !!}
@else
{!! Form::checkbox('language[]', $language->id, false) !!}
@endif
{!! $language->name !!}
@endforeach
/.checkbox -->
/**
* 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 sign in or create an account to participate in this conversation.