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

I'm certainly not a laravel expert, but here's how I'm doing something similar. (adjusted to use your info)

In my controller


public function edit($id)
{
	$profile = Profile::find($id);
	//get all itens
        $iten = Iten::get();
        //just get the itens associated with profile from the pivot table, returned as an array
	$associated_itens = DB::table('profile_iten')->where('profile_id','=',$id)->lists('iten_id');
		
	return View::make('profile.edit', compact('profile','itens','associated_itens'));
}

Then on my edit form I did this for the checkboxes


{{ Form::label('itens', 'Associated Itens') }}
	
@foreach($itens as $key => $val)
	<?php
		$checked = false;
                //as we loop through a list of all itens, we compare to the values retrieved from our pivot table
		if(in_array($val->id, $associated_itens)) $checked = true;
	?>
	{{ Form::checkbox('itens[]', $val->id, $checked) }}
	{{ $val->name}}
			
@endforeach

Hope this helps.

Last updated 1 year ago.
0

Hi, i tried what gregbeatrice posted, but in laravel 5 where it seems no matter what i do my code is overidden and i still get all my checkboxes checked in the edit page. Any other idea? i checked the $checked value and it's correct on all the cases but it seems to be ignored...

i made it work by using another field name in the update form, and on update i did something like:

$item->field = $request->new_field_name;

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sytker sytker Joined 29 Apr 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.