I am just wondering if i could ask a fairly simple question about radio buttons and Laravel as the googlebox isnt co-operating. My code is thus:
{{ Form::open(array('url'=>'users/create', 'method' => 'post')) }}
<tbody>
<label><span> </span>{{ Form::submit('Approve/Decline')}}</label>
#foreach($users as $user)
<tr>
<td>{{{$user->firstname}}}</a></td>
<td>{{{$user->secondname}}}</a></td>
<td>{{{$user->address}}}</a></td>
<td>{{{$user->city}}}</a></td>
<td>{{{$user->phone}}}</a></td>
<td>{{{$user->id}}}</td>
<td>{{{$user->username}}}</a></td>
<td>{{{$user->email}}}</tds>
<td>{{{$user->type}}}</a></td>
<td>{{Form::radio('Membership[]', 'approve')}}</a></td>
<td>{{Form::radio('Membership[]', 'decline')}}</a></td>
</tr>
#endforeach
</tbody>
{{ Form::close() }}
A simple form with my two text boxes at the end there. This turns on fine in the browser but once i check one radio button the others deselect which would be fine in this order:
O O
but since its in a loop, there are many rows of O O and i can only choose one out of all of the different rows. I would imagine I would need to group each two together somehow dynamically but I'm unsure. All aid appreciated.
Probably you'll need to set a different name for each of your checkboxes/radio buttons:
<td>{{Form::radio("Membership[".$user->id."]", 'approve')}}</a></td>
<td>{{Form::radio("Membership[".$user->id."]", 'decline')}}</a></td>
To get your values in your controller you just have to:
dd( Input::get('Membership')[$user->id] );
In return you should get
approve
or
decline
Note that for this to work this way I removed the quotes from
"Membership[".$user->id."]"
Here are two routes I used to test it:
The form:
Route::any('test', function() {
return Form::open(array('url' => 'radio', 'method' => 'post')) .
Form::radio("Membership[1]", 'approve') .
Form::radio("Membership[1]", 'decline') .
Form::submit('send') .
Form::close();
});
The result:
Route::any('radio', function() {
dd( Input::get('Membership')[1] );
});
Related
I am listing many rows in a table and I AM trying to edit them with a drop down menu that exists in each row. The code below is fine but it will store only the last (S_Rank) value (which is the column I want to change) what would be the best way to get each row input. I know the issue that the form is not an array, what would be the way to make it an array
My view
{!! Form::open(['action' => 'AbstractsController#UpdateRank' , 'method' => 'post' ]) !!}
<table id="myTable" class ="table table-striped">
<thead>
<td><h4>Student Name</h4></td>
<td><h4>Student Rank</h4></td>
</thead>
#foreach($applications as $application)
<tbody>
<tr>
<td><h5>{{$application->Student_Name}}</h5></td>
<td><h5>
{{Form::select('Ranking' ,$ranks, ['class' => 'form-control', 'placeholder' =>$application->S_Rank] )}}
{{Form::hidden('Student_ids[]', $application->S_ID)}}
</h5></td>
</tr>
#endforeach
</tbody>
</table>
{{Form::Submit('Save New Ranking',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
My controller
foreach(request('Student_ids') as $S_ID){
$application = Application::where('S_ID' , $S_ID)->get();
$application->S_Rank = $request-> input('Ranking');
$application->save();}
I am trying to update each row with the inputted value from each dropdown menu
I think you already answer yourselve. I think what you need is to change your $ranks var to $ranks[] because you are actually only taking the last value they chose in your select, that way, you are storing your ranks in a one dimenssion array that should correspont with your Student_ids[] hidden array
If you want to update the ranking per application, you'll need to know which ranking value goes to which application. Assuming S_ID is a unique identifier for Application, you could do something like this in your view:
<h5>
{{Form::select('Ranking[' . $application->S_ID . ']' ,$ranks, ['class' => 'form-control', 'placeholder' =>$application->S_Rank] )}}
</h5>
Then in your controller:
foreach(request('Ranking') as $S_ID => $rank){
$application = Application::where('S_ID' , $S_ID)->first();
$application->S_Rank = $rank;
$application->save();}
I am trying to have the user choose a month from a dropdown list displaying the months, and passes that month value (in numbers) to the controller
I am having trouble making the dropdown list an action so once the user picks the month, the controller gets called.
here is my index page:
<h>Display a logs by monthly <h>
{{ $id=Form::selectMonth('month')}}
<a href="{{action('LogController#monthly',['id' => $id]) }}" class="btn btn-primary">Monthly Logs
</a>
and when I add the Form method inside the tag i am getting an error says the variable is not defined.
here is the Controller.php
public function monthly($id)
{
$dcmlogs = log::with('users')
->whereMonth('created_at', '=', $id)
->paginate(15);
return view('dcmlog.index', compact('dcmlogs'));
}
You can use something like this :
{!! Form::open(['route' => ['logs', $id], 'method' => 'POST' ])!!}
{{ Form::select('in_out',[1 => 'Jan', 2 => 'Feb'] , null, ['class'=>'
form-control'])
}}
{!! Form::close() !! }}
hope this helps ..
I recommend creatintg a route
Route::get('/logs/{id}', 'LogController#monthly')->name('logs');
And use the route function in the template:
route('logs', ['id'->$id]);
i have a problem where a checked checkbox is not displaying checked.
My view:
#if(in_array($searchgroup->id.','.$searchtag->id, session()->get('zoektermen')))
{{ Form::checkbox($searchtag->naam,$searchgroup->id. ',' .$searchtag->id, true, ['class' => 'search-checkbox']) }} {{ $searchtag->naam }}
#else
{{ Form::checkbox($searchtag->naam,$searchgroup->id. ',' .$searchtag->id, null,['class' => 'search-checkbox']) }} {{ $searchtag->naam }}
#endif
I'm using laravel collective.
I'm using this code on other places on the website in different scenario's, where this works perfectly and the checkboxes display checked.
I'm using firefox but i also tried safari, no difference.
Any help is appreciated..
Consider the example having checked checkbox or not depending on the say user status Active or Inactive
{!! Form::checkbox('status', 'Active', ($userData->status == 'Active') ? 'checked="checked"' : '' ,['data-prompt-position' => 'centerRight','data-title'=>'Active']) !!}
If you have included "uniform.default.js" in your project, it can create such an issue. Please use the following code to resolve it.
$('#checkbox').prop('checked',true).uniform('refresh');
Further reading: jQuery Uniform Checkbox does not (un)check
Thanks.
so I have a selection box that gives a dropdown menu to give messages a manager from the dropdown. It takes the input and then changes to a column in the database called manager for it's respective column. When I try to submit the selection menu it gives me the regular error for Laravel. But then when I put ?debug=1 at the end it submits but gives the row's manager column a value of just blank.
Here is what I have in the routes.php
Route::get('foo/{id}', 'fooController#bar');
Route::post('foo/{id}', 'fooController#bar');
This is the form.
{{ Form::open(array('url' => '/admin/foo' . $message->id)) }}
{{ Form::select('handler[]', array('unassigned', 'foo', 'bar'), null, array('style' => 'width: 127px')); }}
{{ Form::submit('Change manager') }}
{{ Form::close() }}
{{ $message->manager }}
and here is what is in the fooController
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler;
$message->save();
return Redirect::action('AdminController#foo_bar');
}
I had a problem like this the other day, I have zero recollection of what I did. I really appreciate any help, thanks! The database is postgresql if that's any help
Try a dd(Input::all()) at the beginning of your controller and make sure you're seeing what you expect.
Also since you're sending an array perhaps you have to do Input::get('handler.0') -- see here right below the Input::only() and Input::except() code block.
It would seem as though because you are naming your select handler[], PHP is grabbing it as part of an array.
When setting up your message model, try this...
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler[0];
$message->save();
return Redirect::action('AdminController#foo_bar');
}
Usually, you'd only use names in your forms post-fixed with [] when you are accepting multiple values like checkboxes/multi-selects etc... Otherwise, it's probably best to stick with not using it because it may cause confusion.
I managed to fix it in a almost frustratingly simple way by just changing the method to PUT.
like this
Form::open(array('url' => 'foo/bar', 'method' => 'put'))
I'm having a hard time figuring out how to repopulate a form for edit that has check boxes in it. I think the most confusing part is because they are coming from a pivot table.
I have users, permissions, and users_permissions tables.
For a quick demonstration of what the tables look like, I ran this query and included a screen clip of the results.
return $userPermissions = User::with('permissions')->find($id);
In my form I just have two permissions check boxes for now until I get the concept working, then I will add a foreach loop and grab them all from the database, but now I have the following:
<div class="form-group">
<label>
{{ Form::hidden('permissions[4]', '0', ['class' => 'checkbox-inline']) }}
{{ Form::checkbox('permissions[4]', '1', ['class' => 'checkbox-inline']) }}
Manage Content
</label>
</div>
<div class="form-group">
<label>
{{ Form::hidden('permissions[3]', '0', ['class' => 'checkbox-inline']) }}
{{ Form::checkbox('permissions[3]', '1', ['class' => 'checkbox-inline']) }}
Manage Users
</label>
</div>
I'm not sure if this is useful information, but when I first create the user, I create a permissions array to attach to the new user. Here is the code for that,
public function createUserPermissionsArray($input)
{
$permissionsArray = [];
$permissions = $input['permissions'];
foreach ($permissions as $id => $value)
{
if ($value == 1)
{
array_push($permissionsArray, $id);
}
}
$this->saveNewUserToDatabase($input, $permissionsArray);
}
I really need some direction here about how to solve this problem. Thanks
I recently worked on something similar, except I had groups with permissions assigned to them, which users were then assigned to. For example, for GroupController::edit() I had this code:
public function edit($id)
{
if(!permitted('group.edit')) {
return Redirect::route('user.dashboard');
}
$group = Group::find($id);
$permissions = Permission::all();
return View::make('admin.group.edit', [
'group' => $group,
'permissions' => $permissions,
'assigned' => $group->permissions->lists('id')
]);
}
Then within the form partial for the view (admin.group.partials.form), I had the following code to handle permissions:
#foreach($permissions as $permission)
<div class="row">
#if(isset($assigned))
{{ Form::checkbox(
'permissions[' . $permission->ident .']',
$permission->id,
in_array($permission->id, $assigned))
}}<label for="permissions">{{ $permission->ident }} - {{ $permission->description }}</label>
#else
{{ Form::checkbox(
'permissions[' . $permission->ident .']',
$permission->id)
}}<label for="group">{{ $permission->ident }} - {{ $permission->description }}</label>
#endif
</div>
#endforeach
I've formatted it the best I can, so that it's easy to read. Basically I had a create.blade.php and edit.blade.php which both include the form partial, but each file handles the opening and closing of the form (model binding for edit, normal open for create), hence the if statement.
Unfortunately I couldn't find a better method to achieve this. I hope this helps you.
P.S: As a side note, I've actually written a tutorial about creating a robust and simple ACL with Laravel, I can link if you'd like.