This is my route
Route::delete('/customer_page/summary/{id}', 'ReservationController#delete_cart');
My controller function
public function delete_cart($id)
{
cart::findOrFail($id)->delete();
return redirect('/customer_page/summary');
}
My form
#foreach($cart as $key => $val)
<tr>
<td>{{ $val->room_type }}</td>
<td>{{ $val->number_of_rooms }}</td>
<td>{{ $val->price }}</td>
<td>
<form action="/customer_page/summary/{{ $val->id }}" method="post">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn waves-effect red"><i class="material-icons">delete</i>delete</button>
</form>
</td>
</tr>
#endforeach
I don't know why I always keep getting that error
You need to set your method="POST" on your form, then inside of the form, use Laravel's method_field() helper: {{ method_field('DELETE') }}. Do not attempt to set your method to delete directly.
The other answer using the form helper package form laravel-collective will work fine too, but that's not included by default in Laravel anymore, so I thought it prudent to outline how to achieve this using raw HTML.
Do the following(RESTFUL)
Route::resource('reservations', 'ReservationController');
OR(NON RESTFUL)
Route::delete('delete/{id}',array('uses' => 'ReservationController#destroy', 'as' => 'My.route'));
Controller
public function destroy($id)
{
$item = Reservation::findOrFail($id);
$item->delete();
}
View
#foreach($reservations as $item)
<tr>
<td>{{ $item->description }}</td>
<td>
{{ Form::open(['method' => 'DELETE', 'route' => 'reservations.destroy', $item->id]) }}
{{ Form::hidden('id', $item->id) }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
OR (for non restful)
{{ Form::open(['route' => ['My.route', $value->id], 'method' => 'delete']) }}
<button type="submit">Delete</button>
{{ Form::close() }}
If you are using the below
{!! Form::model($user,
['method' => 'DELETE',
'route' => ['users.destroy', $user->id],
]) !!}
{{ method_field('DELETE') }}
// then you form goes here
{{ method_field('DELETE') }}
{!! Form::close() !!}
You need to change method on form, from POST to DELETE
You see on route you have defined as DELETE:
Route::delete('/customer_page/summary/{id}', 'ReservationController#delete_cart');
Related
I am learning laravel and I have a problem when I call to delete method.
My Routes:
Route::get('/', function () {
return view('inicio');
});
Route::resource('secciones', 'seccionesController');
My form:
{{ Form::open(['route' => ['secciones.destroy', $seccion->id], 'method' => 'DELETE']) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
The $seccion->id is correct, the secciones.destroy (in seccionesController there are a destroy method ) is correct. When I call delete laravel return me:
MethodNotAllowedHttpException in RouteCollection.php line 233:
Can anyone help me?
Thanks in advance..
HTML Forms don't support DELETE method.
But you can use {{ method_field('DELETE') }}
Also, you forgot to add the {{ csrf_field() }} (token field) in the form.
It will be:
{{ Form::open(['route' => ['secciones.destroy', $seccion->id], 'method' => 'DELETE']) }}
{{ method_field('DELETE') }}
{{ csrf_field() }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
Good luck! :)
I have a little problem with passing some data from my view to my controller.
Please have a look at this code snippet:
{!! Form::open(['action' => 'DomainController#detach', 'method' => 'post']) !!}
#foreach($domains as $domain)
<tr>
<td>{{ $domain->name }}</td>
<td>{{ $domain->tld }}</td>
<td id="hello">
#foreach($domain->tags as $tag)
{{ $tag->name }},<br>
#endforeach
</td>
<td>
#foreach($domain->tags as $tag)
{!! Form::hidden('tag_id[]', $tag->id) !!}
<button name="domain_id" value="{{ $domain->id }}" class="glyphicon glyphicon-trash"></button>
<br>
#endforeach
</td>
</tr>
#endforeach
{!! Form::close() !!}
In my Controller is a :
$input = Input::all();
return $input;
In my code ( in the last ) is a button. If I press teh button, I'm getting directed to my controller action. I'm returning the die data in my $input variable and it allways shows me the same tag_id. Allways the very last tag_id of this domain. I don't know why and couldn't figure it out.
It should be:
{!! Form::hidden('tag_id[]', $domain->pivot->id) !!}
you're missing the [] in input
You have 2 fields with the same name:
{!! Form::hidden('tag_id', $domain->pivot->id) !!}
<button name="tag_id" value="{{ $domain->id }}" class="glyphicon glyphicon-trash"></button>
I'm trying to create a form to delete a row in my db:
#foreach ($CostStatuses as $CostStatus)
<tr>
<td class="table-text">
<div>
<a href="/costStatus/{{ $CostStatus->id }}/edit">
{{ $CostStatus->status_name }}
</a>
</div>
</td>
<td>
{{ Form::open(array('url' => 'CostStatus/' . $CostStatus->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete this Nerd', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
But the html shows the form inside the {{ }} as a text to the user.
That's because {{}} does not execute the contents inside. Use {!! !!}.
LAready fixed:
Just change {{ for {!!.
I'm running on Lavarel and crossing this code:
{{ link_to_route('users.edit', 'Edit', array($user->id), array('class' => 'btn btn-info')) }}
This is the ./app/views/users/edit.blade.php
#extends('users.scaffold')
#section('main')
<h1>Edit User</h1>
{{ Form::model($user, array('method' => 'patch', 'route' => array('users.update', $user->id))) }}
<ul>
<li>
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password: ') }}
{{ Form::text('password') }}
</li>
<li>
{{ Form::label('email', 'Email: ') }}
{{ Form::text('email') }}
</li>
<li>
{{ Form::label('phone', 'Phone: ') }}
{{ Form::text('phone') }}
</li>
<li>
{{ Form::label('name', 'Name: ') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
{{ link_to_route('users.show', 'Cancel', $user->id, array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
#if (($errors->any()))
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
#endif
#stop
The above code in the template file edit.blade.php, and when users click to the Edit button, it should pass the user id $user->id to the controller UsersController#edit where edit action is defined,
public function edit($id)
{
$user = User::find($id);
if(is_null($user)) {
return 'Not found: '.$id;
// return Redirect::route('users.index');
}
return Redirect::route('users.edit', compact('user'));
}
The problem here is that $id is not what passing from link_to_route() function.
Can anyone help me find out where the problem is? Thanks.
Here the DOM result:
Edit
This is the routes.php
Route::resource('users', 'UsersController');
This is result after clicking Edit button:
Not found: {"id":1,"username":"john","password":"johndoe","email":"johndoe#gmail.com","phone":"123456","name":"John","created_at":"2013-06-07 08:13:28","updated_at":"2013-06-07 08:13:28"}
Based upon your info - it looks like the problem is not with link_to_route() - you can see the error is further in your code.
It looks like you have bound the route to the model, so in your edit function $id is actually the $user already populated from the database.
You can tell that is the case - because your custom "not found" error gives you the user data.
If you change your code to this - does it work?
public function edit(User $user)
{
return Redirect::route('users.edit', compact('user'));
}
i'm using Blade template Engine in Laravel. and i want to desing forms with twitter bootstrap.
My sample Form
#section('content')
<div style='margin:20px auto;height:100px;width:300px;background-color: #fff;line-height: 1.5px'>
{{ Form::open(array('route'=>'auth', 'method'=>'post')) }}
{{ Form::text('username', Input::old('username'), array('placeholder'=>'Username', 'id'=>'username')) }}
{{ Form::password('password', array('placeholder'=>'Password', 'id'=>'password')) }}
{{ Form::submit('Login', array('id'=>'submit')) }}
{{ Form::close() }}
</div>
#stop
in this sample. how to setting that for bootstrap?
To apply the Bootstrap style to your forms, you need to pass the class "form-control" into your blade:
{{ Form::text('username', Input::old('username'), array('placeholder'=>'Username', 'id'=>'username', 'class' => 'form-control')) }}
Styling laravel/blade forms with bootstrap:
["class"=>"form-control"]