I am beginner at laravel. I want to use laravel collective (on laravel 9 version). And actually i want to use action. I write it in my blade:
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST']) !!}
{!! Form::close() !!}
and when i open it in browser it gives me error Action PostsController#store not defined.
i do not know. help me. i try to googled it but did not find. also i tried [PostsController::class] but not worked. i tried everything what i know
Best Solution - Using named routes
From documentation of Laravel Collectives you may come to know that you can use named routes in your code. So, we better use named route.
{!! Form::open(['route' => 'route.name', 'method' => 'POST']) !!}
{!! Form::close() !!}
Alternatives
You could either string including the namespace of controller with it to work
{!! Form::open(['action' => 'App\Http\Controllers\PostController#store', 'method' => 'POST']) !!}
{!! Form::close() !!}
or you could modify your RouteServiceProvider file and change the controller namespace there. protected $namespace = 'App\\Http\\Controllers'; this approach allows you to use 'ControllerName#method' controllers created in App\Http\Controllers directory.
The other way around is just call the route name.
<form action="{{ route('your.route.name') }}">
...
</form>
Related
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 :)
After clicking in view,
{!! Form::open(['method' => 'GET', 'route' => ['city', $city->id]]) !!}
{!! Form::submit('Check', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
system finds this route:
Route::get('city/{id}', 'CityController#show')->name('city');
which makes URL in web browser look like this:
http://localhost:8888/game/public/city/5?
problem is however, that when I click on another link for example:
<li>Reports </li>
I get URL in following format:
http://localhost:8888/game/public/city/home
instead of:
http://localhost:8888/game/public/home
Which is wrong since it doesn't work. How do I correct it?
Don't use relative URLs. You can use the url helper to generate a fully qualified url.
Home
Reference:
Laravel Docs - Helpers - url method
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
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
// routes.php
Route::resource('/image', 'ImageController');
Route::get('/create', 'ImageController#create');
Route::post('/store', 'ImageController#store');
// create.blade.php
{!! Form::open(array('url' => '/store', 'method'=>'POST')) !!}
.......
{!! Form::close() !!}
Here if i don't write these two lines (Route::get('/create', 'ImageController#create'); Route::post('/store', 'ImageController#store');)
The resource routing of create and store does not work and show some errors.
Why this happens? Thanks in advance.
When creating resource route you don't have to create individual routes. Because all RESTfull default routes will be created for you automatically.
You just need following route
Route::resource('image', 'ImageController');
then change form as below
{!! Form::open(array('route' => array('image.store'))) !!}
.......
{!! Form::close() !!}
Read More