Laravel checkbox $request issue - laravel

I am trying to pass 2 array of checkboxes, to my controller: roles and permissions to insert and/or patch in DB.
in my controller I am passing the collections to display on blade:
$roles = Role::all();
$permissions = Permission::all();
and all works fine as it displays the name and the checkbox.
Yet 2 issues when submitting:
$request is showing a simple array for each without the name of the role/permission.
I do not get an "off" for non checked.
http://prntscr.com/jq61p6
I am expecting to insert/patch roles and permissions for a specific $user->id.
I am assuming the problem is in blade and in my checkbox inputs:
<form method="POST" action="{{ route('roles-permissions',['id' =>$user->id]) }}">
{{ csrf_field() }}
<h5>User Roles</h5>
<div class="col-xs-12">
#foreach ($roles as $role)
{{ Form::checkbox('roles[]', null, true, ['class' => 'name'] ) }}
{{ Form::label($role->name, ucfirst($role->name)) }}<br>
#endforeach
</div>
<div class="break-20"></div>
<h5>User Permissions</h5>
<div class="col-xs-12">
#foreach ($permissions as $permission)
{{ Form::checkbox('permissions[]', null , false, ['class' => 'name']) }}
{{ Form::label($permission->name, ucfirst($permission->name)) }}<br>
#endforeach
</div>
{!! Form::submit('Save Roles and Permissions', ['class=', '"btn btn-primary width-250 mt-20"']); !!}
</form>
How can I fix this so I can see the role and permission name so I can pick up the right permissions/roles by my controller and save them into my roles/user and permissions/user tables?
Thanks.

3rd argument will decide whether it will be checked or not. Use null instead of true & let's check whether it works or not.

Related

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST

I am creating an index form that displays some data. Everything is ready but when I make the delete button I get an error "The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST."
Route
Route::group(['middleware' => ['auth']], function() {
Route::resource('roles','RoleController');
Route::resource('users','UserController');
Route::resource('kamar_theresia','Kamar_TheresiaController');
});
Controller
public function destroy($id)
{
Kamar_Theresia::find($id)->delete();
return redirect()->route('kamar_theresia.index')
->with('success','Kamar Theresia deleted successfully');
}
View
#foreach ($kamar_theresia as $tere)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $tere->nama }}</td>
<td>{{ $tere->name }}</td>
<td>{{ $tere->ketersediaan }}</td>
<td>
#can('theresia-delete')
{!! Form::open(['method' => 'DELETE','route' => ['kamar_theresia.destroy', $tere->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
That's because the you're passing DELETE method as the method of your form, and it is wrong, the right thing to do is pass de POST method.
Check this example:
<form action="{{ route('kamar_theresia.destroy', $tere->id) }}" method="POST">
#csrf
#method('delete')
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
Your controller should be:
public function destroy(Kamar_Theresia $khamar_teresia)
{
$khamar_teresia->delete();
return redirect()->route('kamar_theresia.index')
->with('success','Kamar Theresia deleted successfully');
}
Make sure you do not have your form inside another form. I made this silly mistake and got the same error message.
Looks like you're almost there! I would use POST for the form similar to this:
{{ Form::open(['method' => 'POST', 'route' => ['kamar_theresia.destroy']) }}
{{ Form::hidden('id',$tere->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
and then in your controller
public function destroy(Request $request){
$id = $request->input('id');
Kamar_Theresia::find($id)->delete();
The rest of your code should be ok. Let me know if this doesn't work.
Use the {{ csrf_field() }} and {{ method_field('DELETE') }} into Form.
{{ csrf_field() }}
{{ method_field('DELETE') }}
Use this into Controller
public function destroy($id)
{
$delete = kamar_theresia::find($id);
$delete->delete();
return redirect('/')->with('deleted','Kamar Theresia deleted successfully');
}
if we are using Route::resource() then it will be automatically route with destroy function.
Forgot to put slash in action at start:
<form method="POST" action={{--here=> --}}"/save_edit_delete_order/{{$order_id}}">
#csrf
#method('delete')
......
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Yes, I am</button>
</div>
</div>
</form>
And in resource Controller:
public function destroy($id)
{
return 'kuku';
}
view
<form action="{{route('command.delete',[$command->id,$command->car_id])}}" method="post">
#csrf
{{method_field('delete')}}
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
web
Route::delete('/commands/{commandId}/{carId}/delete','CommandController#deleteUserCommands')->name('command.delete');

Changing laravel form to html form

Hi i created a form with laravel form helpers but i want to change it to a standard html form. The issue i am having is with the "PUT" function, when i try to edit my posts no data is displayed so i think my form properties are wrong.
Form header
<form method="post" action="{{route('posts.update',[$post->id])}}" enctype="multipart/form-data">
{{csrf_field()}}
{{method_field('put')}}
<input type=""text" name="name" class="name">
<input type=""text" name="body" class="body">
<button></button>
</form>
(--UPDATED--)
Laravel Form
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, ["class" => 'form-control input-lg']) }}
{ Form::label('body', 'Body:') }}
{{ Form::text('body', null, ["class" => 'form-control input-lg']) }}
{{ Form::submit('Save Changes', array('class' => 'btn btn-success btn-block')) }}
{{ Form::close() }}
I want the blog data to be displayed in the form for me to edit.. When i use form helpers it works fine
Any help will be much appreciated
Thanks
Ash
Ash,
I don't see the Laravel form code here to compare the two, but the easiest way to convert the Laravel form to straight HTML is to view source on the generated form and copy the generated HTML code back into the blade. Then you can replace populated data with variables.
(--UPDATED--)
Hence, put this back into your blade:
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, ["class" => 'form-control input-lg']) }}
{ Form::label('body', 'Body:') }}
{{ Form::text('body', null, ["class" => 'form-control input-lg']) }}
{{ Form::submit('Save Changes', array('class' => 'btn btn-success btn-block')) }}
{{ Form::close() }}
and then copy the HTML output for the "put" action. Or, if you're on the current version of Laravel, you can simply use the put method as follows:
#method('PUT')
(see https://laravel.com/docs/5.8/blade)
(--ORIGINAL--)
Also, you didn't mention what version of Laravel you're on. The syntax for inserting fields into the blade varies.
(--UPDATED--)
#kapitan, you're right. I "learned" that from using LaraShift telling me that all my {{ $field }} needed to be changed to {!! $field !!} to upgrade my app. The syntax has changed as follows.
In older versions of Laravel, variables inserted with {{ $field }} were UNescaped, and variables inserted with {{{ $field }}} were escaped.
In newer versions of Laravel, variables inserted with {{ $field }} are escaped, and variables inserted with {!! $field !!} are UNescaped. So the meaning of {{ $field }} has reversed from older versions to newer versions.

Resizing checkbox in form group in Laravel

<div class="col-md-3">
<div class="white-box">
{{ Form::open(['route' => ['resources.select'], 'method' => 'ANY', 'role' => 'select']) }}
<div class="form-group">
{{ Form::label('category_label', 'Categories : ') }}
{{ Form::select('category_select', $category_select, ['class'=>'form-control']) }}
{{ Form::button('Submit',['type'=>'submit','class'=>'btn btn-success waves-effect waves-light m-r-10', 'id'=>'select_resource']) }}
</div>
{{ Form::close() }}
</div>
</div>
This is my code. I have three different divisions that look the same. When I tried to change to a grid view, The select boxes stay overlapping the div block.
I want them to stay within the block in the view.
The reason why your select is overlapping is because you are missing null as a parameter in the declaration.
Change this line;
{{ Form::select('category_select', $category_select, ['class'=>'form-control']) }}
to this;
{{ Form::select('category_select', $category_select, null, ['class'=>'form-control']) }}
Also you do not need the to style the form. A more efficient way of creating the form in your question would be like the example below
<div class="col-md-3">
<div class="white-box">
{{ Form::open(['route' => ['resources.select'], 'method' => 'ANY', 'role' => 'select']) }}
<div class="form-group">
{{ Form::label('category_label', 'Categories : ') }}
{{ Form::select('category_select', $category_select, null, ['class'=>'form-control']) }}
</div>
{{ Form::button('Submit',['type'=>'submit','class'=>'btn btn-success waves-effect waves-light m-r-10', 'id'=>'select_resource']) }}
{{ Form::close() }}
</div>
</div>

using the old input of Form::password laravel 4.2

in my blade i have this change password function i wanted it to "display" the old password when the user have errors like for example the user entered nothing and clicked submit so here is the code in my blade
<div class="form-group">
{{ Form::label('cpass', 'Current Password') }} <span style="color:red"><i>{{ $errors->first('cpass', ':message') }}</i></span>
{{ Form::password('cpass', array('class' => 'form-control' , 'placeholder'=>'enter current password') , Input::old('cpass') ) }}
</div>
<div class="form-group">
{{ Form::label('npass', 'New Password') }} <span style="color:red"><i>{{ $errors->first('npass', ':message') }}</i></span>
{{ Form::password('npass', array('class' => 'form-control', 'placeholder'=>'enter new password'), Input::old('npass') ) }}
</div>
<div class="form-group">
{{ Form::label('rpass', 'Re-type new Password') }} <span style="color:red"><i>{{ $errors->first('rpass', ':message') }}</i></span>
{{ Form::password('rpass', array('class' => 'form-control', 'placeholder'=>'re-type new password'), Input::old('rpass') ) }}
</div>
in my controller i throw it like this
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
return Redirect::to('User_Profile')
->withErrors($validator)
->withInput(Input::all());
}
else
{
//do stuff
}
i dont know why im not getting the old input from the user. what may be the wrong im doing? thanks
The form builder password method doesn't accept any input besides the name of the password field and its attributes:
http://laravel.com/api/4.2/Illuminate/Html/FormBuilder.html#method_password
You'll need to do this with javascript by setting the value of the password field manually on page load:
<script type="text/javascript">
$(function()
{
$('#rpass').val("{{ Input::old('rpass') }}");
});
</script>

Printing tasks per task list with Laravel 4 Eloquent

I'm trying to display several task lists in one page with their respective tasks. In my controller I'm using the following:
public function index()
{
$tasks_lists = Task_List::all();
$tasks = Task_List::find(1)->tasks;
return View::make('tasks.index', array(
'tasks' => $tasks,
'tasks_lists' => $tasks_lists
));
}
Using
$tasks = Task_List::find(1)->tasks;
Will return for all tasks lists the tasks appointed to task list 1. So I'm guessing here is my problem, what type of clause should I be using to achieve my goal?
My foreach in my view:
#foreach ($tasks_lists as $task_list)
<h2>{{ $task_list->title }}</h2>
<ul>
#foreach ($tasks as $task)
<li>
{{ Form::open() }}
<input type="checkbox" name="task" value="{{ $task->id }}" />{{ $task->name }}
{{ Form::close() }}
</li>
#endforeach
</ul>
#endforeach
Instead of fetching the tasks for one of the Task_List models (in your case the one with id 1) you should get them for every task_list.
Do this in your view:
#foreach ($tasks_lists as $task_list)
<h2>{{ $task_list->title }}</h2>
<ul>
#foreach ($task_list->tasks as $task)
{{-- etc --}
#endforeach
</ul>
#endforeach
Attention: if you leave it that way it will make a query on the db every time you call ->tasks. You need to use eager loading. This is done by adding with('relationship-name')
$tasks_lists = Task_List::with('tasks')->get();
return View::make('tasks.index', array(
'tasks_lists' => $tasks_lists
));
You don't need to look for the tasks in the controller, so that can be simply removed:
public function index()
{
$tasks_lists = Task_List::all();
$tasks = Task_List::find(1)->tasks;
return View::make('tasks.index', array(
'tasks' => $tasks,
'tasks_lists' => $tasks_lists
));
}
Then, you'll need to get the tasks for each task list in the view, like this:
#foreach ($tasks_lists as $task_list)
<h2>{{ $task_list->title }}</h2>
<ul>
#foreach ($task_list->tasks as $task)
<li>
{{ Form::open() }}
<input type="checkbox" name="task" value="{{ $task->id }}" />{{ $task->name }}
{{ Form::close() }}
</li>
#endforeach
</ul>
#endforeach
And that should be enough. :)

Resources