Laravel 5 Method not found exception - laravel

I use Laravel 5 and try to update a form:
{!! Form::model($user, ['route' => ['edit', $user->id], 'method' => 'PUT']) !!}
{!! Form::label('titel', 'First Name:'!!}
{!! Form::text('titel', null,) !!}
<button type="submit">Update</button>
{!! Form::close() !!}
My route:
Route::post('edit/{id}', ['as' => 'edit', 'uses' => 'UserController#editUser']);
My controller:
public function editUser($id){};
When click on the update Button I get MethodNotAllowedHttpException in RouteCollection.php
I checked in the browser source code and saw that Form::model(..) which I use generate the following output:
<form method="POST" action="http://localhost/myProject/public/edit/1" accept-charset="UTF-8"><input name="_method" type="hidden" value="PUT"><input name="_token" type="hidden" value="4nZlyfzzAZmTcZfThQ8gcR6cgEgYgR0ip0JZTKck">
Within the form there is the attribute method="POST" and the hidden input has the attribute value="PUT". This seems not correct for me. Any ideas? Thank you

You should use 'update' route to actually save the data (validate and persist it to the database). 'edit' route is what you used to generate edit form.
You should use PUT method to run method which saves data.
Also, here is small tip for you. Learn about how RESTful controllers work. They are really easy way to do what you're doing here (defenetly worth to learn them):
https://laravel.com/docs/5.1/controllers

Your route is not the same as your form.
Laravel uses the hidden inputs to specify the different http methods, as put.
So in your routes you should use a put method not a post.
Route::put();

Related

Laravel: REJECT ROUTE not defined but exists in web.php

I have a reject function in my Calendar controller but whenever I redirect to the view page it displays an error saying my route is not defined.
I've tried rearranging and renaming my route but it's still displaying the error.
Here is my form:
{!! Form::open(['url' => route('therapist.reject.appointment', $bookingRequest), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
{{csrf_field()}}
{!! Form::close() !!}
Here are my routes. The other routes displayed are working perfectly:
Route::get('therapist-calendar/{bookingRequest}', 'TherapistCalander')->name('therapist.calendar');
Route::post('therapist-calendar/{bookingRequest}',
'TherapistCalander#saveAppointment')->name('therapist.book.appointment');
Route::patch('therapist-calendar/{bookingRequest}',
'TherapistCalander#finishedAppointment')->name('therapist.finish.appointment');
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#cancelAppointment')->name('therapist.cancel.appointment');
And lastly, my function:
public function rejectAppointment(Request $request, BookingRequest $bookingRequest)
{
$bookingRequest->reject();
return redirect()->back()->with('rejectStatus', true);
}
The view page where this button belongs should be able to display the buttons for rejecting and finishing, alongside the calendar view.
EDIT
Follow up question: Is it possibly because the routes are similar to one another? If so, how do I fix this?
Try to change the Reject and Cancel the url string because it is similar.
Route::delete(
'therapist-calendar/{bookingRequest}/delete',
'TherapistCalander#rejectAppointment'
)->name('therapist.reject.appointment');
Route::delete(
'therapist-calendar/{bookingRequest}',
'TherapistCalander#cancelAppointment'
)->name('therapist.cancel.appointment');
Change your code to
{!! Form::open(['url' => route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
{{csrf_field()}}
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
{!! Form::close() !!}
The route parameters are passed as an array and that should work fine. Refer doc
Can you try this code
<form action="{{ route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]) }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
</form>
UPDATE
PROBLEM FIXED
I realized since they have similar links, web.php found it confusing so it did not read this route.
That is why I changed my route from:
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');
To this:
Route::delete('doReject/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');

Form blade, can't understand translating from blade to html

I have this form in blade:
{{ Form::open(['action' => ['SearchController#searchUser'], 'method' => 'GET']) }}
{{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter name'])}}
{{ Form::submit('Search', array('class' => 'button expand')) }}
{{ Form::close() }}
How can I translate it into html?
If you're using Laravel 5.2, you must install HTML and Form as module.
follow these steps and it must be rendered your code:
https://laravelcollective.com/docs/5.2/html
If you are new to Laravel and feel its confusing with all those syntax you can use normal HTML that you already know. Its not a compulsion to use Laravel's blade shortcut.So something like
<form action="SearchController#searchUser" method="get">
//All your inputs
</form>
And to use form in your project you have to get a facade. You can follow the tutorial below :
http://www.easylaravelbook.com/blog/2015/02/09/creating-a-contact-form-in-laravel-5-using-the-form-request-feature/

How can I include html within a form label using Laravel Collective?

Reading through this SO thread I have read that I can create a new macro to create custom form inputs.
I am brand new to Laravel development (big surprise) & it seems a tad overkill for such a small thing. Is there a "simpler" way to have something like this:
blade template
{!!Form::label('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}
html
<label for="firstName">First Name*</label>
<input type="text" name="firstName" class="required">
Or, is this a case where I just write the html, and then use the form service to create the inputs?
Thank you for your patience and insight.
The simple way to do this is
{!! Form::label('labelFor','labelText',[],false) !!}
the last parameter is $escape_html which default value is "true".
The Form class attributes will always be escaped (they were in Laravel 4 and still are with Laravel Collective's 5+ support), therefore no HTML is allowed. Since you're needs are so simple, I'd suggest just writing plain HTML.
If you want overkill, maybe something like this in your AppServiceProvider.php:
Form::macro('labelWithHTML', function ($name, $html) {
return '<label for="'.$name.'">'.$html.'</label>';
});
Then, in your Blade templates:
{!! Form::labelWithHTML('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}
{!! Html::decode(Form::label('email', 'E-Mail Address', ['class' => 'text-muted'])) !!}
that is much better to fix these type of problems
Maybe it's late to answer, but you could do this:
{!! Html::decode(Form::label('firstName','FirstName: <sup>*</sup>')) !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}

Laravel white page loading route

I had a login.blade.php with some code, and I decided to replace with a new one.
I renamed it to old_login.blade.php and create a new file, in the same path, with the name login.blade.php.
After a while, I decided to rollback to my old login page.
I delete login.blade.php, and renamed the old_login.blad.php with the original name to return back.
No code was edited, only views.
The problem is that the page returned a blanck white page with many comments (the comment's tag is not closed).
I try to make a copy of the view, called test.blade.php, and change the route to that view. It diplay them correctly. But If I change another time route to myapp.login to display login.blade.php view, it won't work.
I try anything but nothing is changed. I'm using laravel 5.1.
The code insiede my routes.php is
Route::get('/', function () {
return view('myapp.login');
});
the code inside the login.blade.php (same ad test.blade.php) is:
#extends("app")
#section("content")
{!! Form::open(["url" => "home"]) !!}
<div class="form-group">
{!!Form::label("username", "Username:") !!}
{!!Form::text("usr_username", "Luca", ["class" => "form-control"]) !!}
</div>
<div class="form-group">
{!!Form::label("password", "Password:") !!}
{!!Form::input("password", "usr_password", "mypass", ["class" => "form-control"]) !!}
</div>
<div class="form-group">
{!!Form::submit("Login", ["class" => "btn-primary form-control"]) !!}
</div>
{!! Form::close() !!}
#include("errors.list")
#stop
So a nonsense answer to resolve my problem: I cutted the code inside login.blade.php, save file, pasted it, and saved another time. Now it display correctly.

Laravel form won't PATCH, only POST - nested RESTfull Controllers, MethodNotAllowedHttpException

I am trying to allow users to edit their playlist. However, whenever I try to execute the PATCH request, I get the MethodNotAllowedHttpException error. (it is expecting a POST)
I have set up RESTful Resource Controllers:
Routes.php:
Route::resource('users', 'UsersController');
Route::resource('users.playlists', 'PlaylistsController');
This should give me access to: (as displayed through php artisan routes)
URI | Name | Action
PATCH users/{users}/playlists/{playlists} | users.playlists.update | PlaylistsController#update
However, when I try to execute the following form, I get the MethodNotAllowedHttpException error:
/users/testuser/playlists/1/edit
{{ Form::open(['route' => ['users.playlists.update', $playlist->id], 'method' => 'PATCH' ]) }}
{{ Form::text('title', $playlist->title) }}
{{ Form::close() }}
If I remove 'method'=> 'PATCH' I don't get an error, but it executes my public function store() and not my public function update()
In Laravel 5 and up:
<form method="POST" action="patchlink">
#method('patch')
. . .
</form>
Write {!! method_field('patch') !!} after form:
<form method="POST" action="patchlink">
{!! method_field('patch') !!}
. . .
</form>
Official documentation for helper function method_field()
Since html forms support only GET and POST you need to add an extra hidden field
to the form called _method in order to simulate a PATCH request
<input type="hidden" name="_method" value="PATCH">
As suggested by #Michael A in the comment above, send it as a POST
<form method="POST" action="patchlink">
<input type="hidden" name="_method" value="PATCH">
Worked for me.

Resources