Support the ongoing development of Laravel.io →
Input Blade Forms
Last updated 1 year ago.
0

Yes.
I use this technic to work with Many-to-Many relationships.
Let's say you have a role-permission model.

<div class = "form-group">
	{{ Form::label('Permissions') }}
	<?php $permissions = Permission::all();
	$allowed = $role->permissions->lists('id');
	$value = false;?>
	@foreach($permissions as $permission)
	<?php $value = in_array($permission->id, $allowed);?>
	<div class = "checkbox">
		<label>
			{{ Form::checkbox('allowed[]', $permission->id, $value) }} {{ $permission->ime }}
		</label>
	</div>
	@endforeach
</div>

Inside the controller I have

$input = Input::all();
        if(isset($input['allowed']))
            $allowed = $input['allowed'];
        else $allowed = array();
        if(!$allowed || !is_array($allowed))
            $allowed = array();

        if(count($allowed) > 0)
            $allowed = Permission::select('id')
            ->whereIn('id', $allowed)
            ->get()
            ->lists('id');

        if(count($allowed) > 0)
            $this->permissions()->sync($allowed);
        else $this->permissions()->detach();

I hope this is what you were looking for.

Last updated 1 year ago.
0

Thanks man! Nice Solution!

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

gstrobl gstrobl Joined 14 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.