Passing data from blade view to controller in laravel - php

I use Form::open(['action' => 'Controller#method']); in blade to pass data from view to controller. Then I got an error:
Action App\Http\Controllers\Controller#method not defined.
That's right since my Controller is at this address:
App\Modules\Somethings\Controllers\
So, how can I do to fix it.
Thank you.

You can fix it by using routes and not controller#method.
In your routes.php define a resource pointing at your controller
Route::resource('posts', 'PostController', ['before' => 'csrf']);
Then you can use the URL::route() method to get the right route.
{!! Form::open(['url' => URL::route('posts.update', [$post->id]), 'method' => 'put', 'files' => false]) !!}
To see all your routes and how they are aliased run the following artisan command:
L5.x
php artisan route:list
L4.x
php artisan routes

you can try this way.
first create a route for this in routes.php file
Route::any('first/second/{id}', [
'uses' => 'App\Modules\Somethings\Controllers#method'
]);
Then use that routes url in form
{!! Form::open(['url' => 'first/second/5', 'method' => 'put']) !!}

May be the file name is problem. Laravel is basically provide default Controller also. Why not you change the name and try with that new name or full path.
You will get routes file from routes folder also and use laravel 5.3

You can use
Form::model
instead.
Example:
Form::model($user, ['method' => 'PATCH', 'action' => ['UserController#update', $user->id], 'files' => true])
// do you form stuff
Form::close()

Related

I am receiving the Laravel error "MethodNotAllowedHttpException in RouteCollection.php line x" while trying to POST a query form

It's just a query form. I searched for this problem and tried the many solutions I found, but none of them solved this problem.
Laravel version: 5.4.23. Using Blade template engine.
My route:
Route::post('products/search', 'ProductsController#search');
My form:
{{Form::open(['url' => ['products/search']])}}
{{Form::text('search', $search)}}
{{Form::submit('Search')}}
{{Form::close()}}
My controller:
class ProductsController extends Controller
{
...
public function search(Request $request) {
dd('This point is never reached.');
return view(...
}
}
Notes:
I tried to change the route to Route::get, but not worked. Tried to change my form method to GET, as {{Form::open(['method' => 'GET', 'url' => ['products/search']])}}, with no success.
I don't know the framework but why is the url value in the form an array?
I would have thought you'd want something like
Form::open(['url' => 'products/search'])
According to the docs, the default form method is POST so you should keep your route as Route::post. If you want to change the method to GET (which I would recommend for a search action), simply use Route::get and
Form::open(['url' => 'products/search', 'method' => 'get'])
Seems you can also use named route names or even controller methods via the action property, ie
Form::open(['action' => 'ProductsController#search'])
I think you need to change in your blade file like:
{{Form::open(['url' => ['products/search']])}}
TO
{{Form::open(['url' => 'products/search'])}}
OR you can add route name in your route like :
Route::post('products/search', 'ProductsController#search')->name('products.search');
{{ Form::open(['route' => 'products.search', 'method' => 'post']) }}
Hope this help you
So the issue might be:
Solution1. You have another route defined on top of that, something like: Route::get('products/{wildcard}', 'Controller'} in that case make sure you put all the other routes under the one you're trying to get to work.
Solution2
Go ahead and delete the controller. Then run composer dump-autoload
Then run the command on command line php artisan make:controller UserController then paste profile method that you have.
Solution3 You might have cached the routes so do: php artisan route:clear
Solution 4. change url to
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}

Route [user.update] not defined

i tried these two methods..
first one : MethodNotAllowedHttpException
Route::post('/settings/{id}/update/', 'HomeController#update');
Route::match(['put','patch'], '/settings/{id}/update/','HomeController#update') use this also..
{!! Form::model($user, ['method' => 'patch','action' => ['HomeController#update',$user->id]]) !!}
another one
{!! Form::model($user, ['method' => 'patch','route' => ['user.update',$user->id]]) !!}
please explain how to use route for update default auth users.
You should give a name to the route:
Route::patch('/settings/{id}/update/', 'HomeController#update')->name('user.update');
Or:
Route::patch('/settings/{id}/update/', ['as' => 'user.update', 'uses' => 'HomeController#update']);
I think you should just be specific about the method you want to use, be it put or patch and also If i remember correctly, if you have to use patch method referencing the answer from this post: Laravel form won't PATCH, only POST - nested RESTfull Controllers, MethodNotAllowedHttpException
<form method="POST" action="patchlink">
{!! method_field('patch') !!}
. . .
</form>
The method field is required because as I understood, Laravel uses this mechanism to handle patch request.
PS: What I just tried to highlight if I understood correctly is that, there should be an extra field to handle the patch method.
Hope this helps :)

Define controller method as form action

I have a following code
{!! Form::open(array('action' => 'clientController#create')); !!}
it shouts that clientController#create is not defined unless I put some code into routes.php (this is my solution)
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
Everything then works fine, but I am not sure if I have found the correct solution, do I have to create a route for every form that will be created? I don't know why Laravel could not find a controller method without registering it in routes.php file
When you are calling an action Laravel searches through it's stored route to handler associations. This is done in routes.php
If you insert
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
in your routes.php you can do any of the following
{!! Form::open(array('action' => 'registration')); !!}
{!! Form::open(array('action' => 'clientController#create')); !!}
If you only stick with
Route::post('clientRegistration', 'clientController#create');
you can still use
{!! Form::open(array('action' => 'clientController#create')); !!}
More information here: https://laravel.com/docs/5.2/routing#named-routes
No matter what, you will need to inform Laravel about your route in routes.php, because otherwise it will not know how to handle the request.
I suggest
Route::resource('client', 'ClientController');
Please read
https://laravel.com/docs/5.2/controllers#restful-naming-resource-routes

Unable to create form tag in Laravel 5

Directory structure.
My current code is below
{{ Form::open(array('action' => 'SkillsController#store', 'method' => 'POST' }}
{{ Form::close() }}
I got the following error
Class 'Form' not found
{{!! Form::open(array('action' => 'SkillsController#store', 'method' => 'POST'}}
{{!! Form::close() !!}}
Still I am facing the same issue.
Reference
As of Laravel 5, Form helpers were removed and are now maintained and provided by the Laravel Collective.
The steps provided in their docs:
Add "laravelcollective/html": "5.1.*" to the require section of your composer.json file
Run composer update
Add Collective\Html\HtmlServiceProvider::class, to your array of providers in the congif/app.php file
Add 'Form' => Collective\Html\FormFacade::class, and 'Html' => Collective\Html\HtmlFacade::class, to your array of aliases in the config/app.php file
You'll then be able to use them in your views using {!! !!} in your views, like so:
{!! Form::open(array('url' => 'foo/bar')) !!}
//
{!! Form::close() !!}
In laravel 5 HTML, and forms not included you should include manually, can follow instructions in this link.
http://laravel.io/forum/09-20-2014-html-form-class-not-found-in-laravel-5

Laravel 5 form URL creation

Working on trying to get the following form's URL to populate properly. Been stumbling over this for some time so here for some help.
As you can see from the following code, I am opening the form - binding the model - and trying to set the URL dynamically. the full URL is something like {username}/account/cards/id so i need to pass it the username (which i would like to pass the authenticated user (as they would only have access to their own page) and the ID of the card they are trying to update.
{!! Form::model($card, ['method' => 'PATCH', 'action' => 'Account\CardsController#update', array(Auth::user()->username, $card->id) ]) !!}
Now this is all happening in blade (front end) so not 100% what i am doing wrong. I have tried action, url, route... I can not get anything to work for some reason. Error I am getting on this one specifically is a array to string error. But if I can't build an array how do i pass in multiple variables? So a bit confused here.
any help would be appreciated.
Thanks
Citti
This is an update to my previous answer. You can try this one:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', Auth::user()->username, $card->id] ]) !!}
You're just passing your route parameters as an option to the Form::model tag, not the route. Try:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', [Auth::user()->username, $card->id] ] ]) !!}
If you're still having trouble I would suggest you name your route and reference the named route in the action.
If I'm not mistaken, the action should be an array if you are passing variables through to the controller. Ex:
{!! Form::model($card, array('method' => 'PATCH', 'action' => array('Account\CardsController#update', Auth::user()->username, $card->id))) !!}
Not sure if you are using Larvel Collective HTML and Forms, but they are essentially the same as the Laravel version. This page: http://laravelcollective.com/docs/5.0/html#form-model-binding explains more about your particular use case.
Hope it helps.
P.S. Try adding:
{!! Form::hidden('_method', 'PATCH') !!}
...underneath the open tag instead of within it. This has to do with L5 method spoofing, and is generally necessary for anything that is not 'POST' or 'GET', I believe. (i.e., 'PUT', 'PATCH', and 'DELETE')

Categories