Laravel 5.4 csrf_field() not working - php

I am trying to make a simple view and I want to be able to make a post request. I need to generate a csrf token and this is the html view that I have:
<form method="POST" action="/formsubmit">
{!! csrf_field() !!}
First Name: <br>
<input type="text" name="firstname"><br>
Last Name:<br>s
<input type="text" name="lastname"><br>
<input type="submit" name="Submit"><br>
</form>
This is my route:
Route::get('form', function(){
return view('form');
});
Route::post('formsubmit',function(){
return 'Form Posted.';
});
Auth::routes();
This is what happens when I try the url:
As you can see, it just prints the name of the function, but the function is not ever called and the hidden field is not being generated. Is there something that needs to be done in order to make it work?

Blade's templating language will only get interpreted in a file with a .blade.php extension. One with just a .php extension will work with Laravel, but you won't have any Blade functionality.

Related

NotFoundHttpException en laravel 5.2

i'm using laravel 5.2 and i'm trying to define routes, but i don't know why i get NotFoundHttpException when i try to do post.
<form action="POST" action="post_to_me">
<input type="text" name="name" >
<input type="submit">
</form>
this is my form, very simple.
route::post('post_to_me',function(){
echo "post";
});
here i define the route to post, but when i run tha app, NotFoundHttpException comes out. Does anyone know why?
I think you have missed adding the CSRF Token to your form. Try adding
// Vanilla PHP
<?php echo csrf_field(); ?>
or
// Blade Template Syntax
{{ csrf_field() }}
just below your form tag

form update with file upload laravel

I'm having a bit of an issue when it comes to updating a form and and having an file input. Here is what I am working with.
I have a form in laravel 5.1 which has a post method and a hidden 'Patch' method. This works as is should updating the fields that are in the form. However, when it introduce:
<input type="file" id="profile_picture" name="image_url" />
into the form, i get a:
MethodNotAllowedHttpException in RouteCollection.php line 218:
laravel error. I have tried changing the
<input type='hidden' name='_method' value='PATCH'>
to PUT and it still doesnt like it.
My form looks like this:
<form action='{{url("profiles/$user->id")}}' method="post" class="form-horizontal" enctype="multipart/form-data">
route resource looks like this:
Route::resource('profiles', 'ProfilesController');
I can't figure out what I am missing here...Any help is much appreciated.
I believe it has to do with the exact route you are typing out in the "action" parameter matching up with the profile controller's update method.
Try changing
action'{{url("profiles/$user->id")}}'
to
action='{{ route("profiles.update", $user->id) }}'
Additionally, you could use the Laravel Collective HTML package to simply opening and closing of forms.
Also for POST Request types, you need to send the CSRF token along with your form data. If you are using laravel blade template in your view, you may use
{{ csrf_field() }}
which translates to
<input type="hidden" name="_token" value={{ csrf_token() }}
Please refer the documentation for this.

getting laravel local host path error (object not found)

I'm new to laravel and still learning about this framework.
I already found some questions on stackoverflow but it still didn't work out for me.
My problem is:
I got this
localhost/codehub/public/users/create
and the route:
Route::get('users/create',['uses' => 'UserController#create']);
Inside the page there's some form like this:
so when I click create button it is supposed to route it into store function in the user controller
Route::post('users',['uses' => 'UserController#store']);
public function store(Request $request)
{
return $request->all();
}
so the problem is when I click that create button it always redirects me to localhost/users and because of that, I can't process my store function.
Any advice?
this is my form code:
<form method="post" action="/users">
<input type="text" name="name">
<input type="email" name="email">
<input type="password" name="password">
<input type="submit" value="Create">
</form>
The problem may be because of relative path in form action.
You should always use named routes which allow the convenient way of generation of URLs or redirects for specific routes.
So you can change your route as:
Route::post('users', 'UserController#store')->name('users.create');
And in form you can write as:
<form method="post" action="{{ route('users.create') }}">

Form submits as GET Laravel 4

I have form like this
<form action="{{ Request::root() }}/articles/update/" method="post">
<input type="hidden" name="id" value="{{ $article->id }}" />
<input type="submit" name="submit" value="Submit" />
</form>
And route like this
Route::post('articles/update', array('as' => 'articleUpdate', 'uses' => 'ArticlesController#update'));
But when I submit the form, I get MethodNotAllowedHttpException. In error report I can see that request method is GET. I have also tried using caps for method method="POST" but it didn't work.
Any ideas?
What does FireBug/Web console inspector show you? is the form being sent via GET or POST, any redirects?
Seems a redirection problem to me, after reaching the server Laravel redirects to the URL the form sent the post request.
you must use put method here. Form change like this
{{Form::open(array('url'=>'/articles/update','method' => 'PUT'))}}
Routes like this
Route::put('/articles/update','ArticlesController#update');

Laravel - Generate route to the delete method through link

I have a PostController in which I have all RESTful methods. I can generate route to delete method by defining method in form tag like below,
<form action="{{route('post.destroy', [$post->id])}}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="Delete">
</form>
But I need to generate same route with link,
Delete
thanks.
You have to create an link, which triggers your Form Submission with JavaScript. You cant create an Link with "POST" instead of "GET" Method.
As Basic example without JQuery and proper separation of code, add this to your template.
Add an ID to you Form "myform"
<script type="text/javascript">
function submitMyform()
{
document.myform.submit();
}
</script>
And change your link to:
Delete
if you want to show only the link, use hidden form elements.
This should do it:
Delete

Categories