How to submit a form using PUT http verb in Laravel - php

I know that this question may have been made but I just can't get it to work. if someone could help me I would be very grateful. I have colletive/form installed but the answer can be an html form tag too.
Now listing my form, my route and my exception.
{{ Form::model( array('route' => array('casas.update', 238), 'method' => 'PUT')) }}
<input type="hidden" name="_method" value="PUT">
-
Route::resource('casas', 'CasasController');
exception:
MethodNotAllowedHttpException in RouteCollection.php line 218:

With plain html / blade
<form action="{{ route('casas.update', $casa->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}
{{-- Your form fields go here --}}
<input type="submit" value="Update">
</form>
Wirth Laravel Collective it may look like
{{ Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put']) }}
{{-- Your form fields go here --}}
{{ Form::submit('Update') }}
{{ Form::close() }}
In both cases it's assumed that you pass a model instance $casa into your blade template
In your controller
class CasasController extends Controller
{
public function edit(Casa $casa) // type hint your Model
{
return view('casas.edit')
->with('casa', $casa);
}
public function update(Request $request, Casa $casa) // type hint your Model
{
dd($casa, $request->all());
}
}

Related

I want to use the Delete route, but it is not supported. How can I use this route?

I want to delete the record selected from the index list, but I'm having trouble using deleteRoute
web.php
Route::delete('/destroy/{id}', 'ProjectController#destroy')->name('despro');
Controller
public function destroy($id)
{
Project::destroy($id);
return redirect('/project');
}
index.blade.php
#foreach($view as $v)
<tr>
<td>{{$v->id}}</td>
<td>{{$v->project_name}}</td>
<td>{{$v->name}}</td>
<td>{{$v->division}}</td>
<td>{{$v->content}}</td>
<td>{{$v->date}}</td>
<td>{{$v->preferred_date}}</td>
<td>{{$v->user_name}}</td>
<td>
#foreach($cats as $cat)
#if($v->category_id == $cat->id)
{{$cat->name}}
#endif
#endforeach
</td>
<td>{{$v->status}}</td>
<td>{{$v->estimated_work_time}}</td>
<td>delete</td>
</tr>
#endforeach
Will be deleted and redirected to index.blade.php but
The GET method is not supported for this route. Supported methods: DELETE.
Come out
delete route require DELETE method.
If you're using Laravel 5.1 or later
<form action="{{ route('despro', ['id' => $v->id]) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button>delete</button>
</form>
If you're using Laravel 5.6 or later then
<form action="{{ route('despro', ['id' => $v->id]) }}" method="POST">
#method('DELETE')
#csrf
<button>delete</button>
</form>

Post Method request not found

I'm having a problem with my code now and it seems like that the route that I specify is not found whenever I try to access it.
Route:
Route::post('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
Route::get('nniscaseassociates/{id}/reliever', 'NnisCaseAssociateController#reliever');
View:
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
Controller:
public function reliever($id)
{
//this will be nniscase id then
$caseassociate = NnisCaseAssociate::findOrFail($id);
//return dd($caseassociate);
return view('nniscaseassociates.reliever', compact('caseassociate'));
}
public function pushreliever(Request $request, $id)
{
...Statements...
return redirect('nniscases/'.$caseassociates->nnis_case_id.'/edit');
}
By the end of submitting I want to redirect to my edit page and display the changes that I made from the previous form. And this is the error that I got when click submit.
You are making a PUT request and not POST.
Either remove this line from your form:
{{ method_field('PUT') }}
Or
Change your POST route to PUT:
Route::put
You have not defined PUT method in your web.php
Route::put('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
in your view :
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
in your controller :
public function pushreliever(Request $request, $id)
{
echo $id;
}
You should try this:
<form method="POST" action="{{ url('nniscaseassociates/pushreliever',[$caseassociate->nnis_case_id]) }}">

delete function Laravel 5.2

I've been learning laravel 5.2 recently, and i've made a delete function which should delete records from my database but instead of deleteing the records it's adding a blank row into my database
This is the Route im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'destroy', 'edit', 'update', 'create']]);
This is the controller function i use for it
public function destroy(request $request , product $product)
{
$product->delete();
return redirect(Route('producten.index'));
}
This is the form i've made for it.
{{ Form::Open(['Route' => 'producten.destroy', $product], ['method' => 'delete']) }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
when i viewed the source-code it said it was using a POST method instead of a delete method, and also when i add($product) i got a blank page, also i found out that when i hit the submit button it goes to the store method i've made and i dont know why,
if u need more information just let me know and i'll add it in the question
route and method should be in the same array, not in two differents arrays.
{{ Form::Open(['method' => 'DELETE', 'route' => ['producten.destroy', $product]]) }}
{{ method_field('DELETE') }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
I think you have something wrong with form. Can you try with this:
<form action="{{ route('producten.destroy', ['product' => $product->id]) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Remove</button>
</form>

Why is my laravel 5 rendering html as string instead of dom?

So I have written the following route:
Route::get('/login', function() {
return View::make('login.form');
});
This is the view:
#extends('layouts.master')
#section('content')
<div class="form-section">
{{ Form::open(
array(
'url' => 'login-submit',
'method' => 'POST'
)
)
}}
{{ Form::submit('Authorize With AisisPlatform') }}
{{ Form::close() }}
#stop
This is exactly what I see when I look at the page:
<form method="POST" action="http://app-response.tracking/login-submit" accept-charset="UTF-8"><input name="_token" type="hidden" value="7xHzX20h1RZBnkTP2CRraZVsAfSQIfVP61mBiFtN"> <input type="submit" value="Authorize With AisisPlatform"> </form>
Um..... Shouldn't the form be well .... and actual form? Why did it render out the html as a string? How do I make it render the actual form submit button?
The default echo braces: {{ ... }} escape HTML by default, to prevent HTML injection.
You should use {!! .. !!} to print raw HTML. For example:
{!! Form::submit('Authorize With AisisPlatform') !!}

Form file upload protected properties

I can not handle file upload forms. Sorry if it is a dummy question, but:
If I use 'files' => 'true' or 'enctype' => 'multipart/form-data' in the Forms open tag I get an object with protected properties. How can I handle the originalName, mimeType etc.. in my app?
To handle file uploads you do:
On your view:
<form action="{{ UR::route('upload') }}" method="POST" enctype="multipart/form-data">
<input type="file" name="photo" />
<input type="submit" value="Upload!">
</form>
Or in blade:
{{ Form::open(array('url' => UR::route('upload'))) }}
{{ Form::file('photo'); }}
{{ Form::submit('Upload!'); }}
{{ Form::close() }}
Then on your controller you can:
$name = Input::get('photo')->getFileName();
$size = Input::get('photo')->getClientSize();
Input::get('photo')->move(public_path().'/uploads', $name);
You can find a full list of methods in the file vendor\symfony\http-foundation\Symfony\Component\HttpFoundation\File\UploadedFile.php

Categories