Issue passing parameters Form::Open Laravel - php

Fairly new at Laravel and trying to grasp the store function and routing. I'm getting the following error trying to open a form in Laravel:
Missing required parameters for [Route: {$route->getName()}] [URI: {$route->uri()}].
The issue is because my url is:
/projects/1/documents/create
And I'm opening to:
/projects/1/documents
I'm trying to pass the projects ID but I'm missing something.
Form Call:
{!! Form::open(['route'=>'projects.documents.store', $project->id]) !!}
#include('pages.projects.documents.charter.partials._form', ['submitButtonText'=>'Create Project Charter'])
{!! Form::close() !!}
My Web Route (I'm assuming the issue is here):
// Resource route for Project Document Controller
Route::resource('projects.documents', 'Project\DocumentController');
My DocumentController store function:
public function store(Request $request, Project $project)
{
// Validate the request
}
I'm not sure if there is any other sections of code that are need. The page renders fine without Form::open and $project->id echo's out correctly.
Edit:
I figured out my issue, it was somewhat silly. Just not use to Laravel formatting yet. The route & param needed to be in an array. The correct formatting is:
{!! Form::open(['route'=>array('projects.documents.store', $project->id)]) !!}

Related

MethodNotAllowedException when send post form with csrf_token Laravel 5.4

I'm trying to access my "store" (POST) method from my route resource. I call the ProductCRUD.store method from a form that can have different input depending on the previous page. I mean by that, depending if the object i receive have certain boolean value, the input is shown.
#if($category->havePrice == 1)
{!! Form::label('price', 'Price') !!}
{!! Form::text('price', '', array('id' => 'price', 'name' => "price") !!}
#endif
When I click on the button to send the form, I got a message saying
MethodNotAllowedHttpException in RouteCollection.php line 251:...
I tried to add a csrf token with the method csrf_field() too.
I tried to empty the cache of laravel with those two methods:
php artisan cache:clear
php artisan route:cache
I tried to create a personal route like this:
Route::post('/product/store', array('as' => 'product', 'uses' => 'ProductController#store'));
None of those solution works. Always that exception.
My current code:
Route:
Route::resource('ProductCRUD','ProductController');
Form
<form method="POST" action="{{route('ProductCRUD.store')}}">
{{ csrf_field() }}
Controller method:
public function store(Request $request) {
$newGameModel= new GameModel();
$category = $this->CategoryRepo->getByID($request->category);
$this->validate($request, [
'name'=> 'required',
'picture'=> 'required|image|mimes:jpg,jpeg,gif,png|max:5000',
'quantite' => 'required|min:0',
'price' => 'required_if:'.$category->havePrice.',1',
]);
...
}
If I remove the "$this->validate(...)" method the code continues to execute (no more MethodNotAllowedException error). I tried to comment one by one validation it changes nothing.
Plus, if I reload the page, the input error are shown correctly. If i click again to send the form, surprise! The error again.
Edit:
I send my form using only a button and the form, no ajax:
{!! Form::submit('Next step', 'name'=>"accept", 'methode' => "post")) !!}
Plus, I can access my controller code. If I remove the validate method, everything work find
Do you guys have any idea why it's doing that? Thank you for your time!
Ok, so I found the problem and how to fix it (not what I wanted to do).
First, I don't have any error in my code. My post, csrf and my routes are all good.
The problem is how larval validates things. For example, If your first page was a post form and your second page is also a post form, when you validate on the second page and it fails, it will try to return to the second page to show the error. But the fact is, it can't find the response of your first page, so you got an error like I got.
I fix it by removing the first form (it was, for me, a little information I can put somewhere else). But I don't know if there is a better way to fix it.

Laravel blade "{{ command }}" not working correctly

I have this weird problem with my Laravel 5.5 version... I created the auth views using
php artisan make:auth
This command created the views controllers and everything I need to lets get stared to work. But I'm having this visualization problem
As you can see on the register view I have this problem.
The real thing is that "{{ any_command }}" is printing the code that its supose to generate instead of interprating like part of the code. But if I use {!! any_command !!} instead it seems to work propertly. What can happend to my laravel is screwed up. It has nothing to be with the artisan auth method, because I tried to create a new form (using laravel collective form helper) and get the same result.
{{ }} will escape all data before printing. So, if you write any HTML tag inside, it will be escaped and printed as is. Just like your example.
{!! !!} will print unescaped data. This will print the tags correctly, but you have to take care where you use it, because someone could inject unwanted data there.
So, in your case, you should use {!! !!}.
Please, refer to this question: What is the difference between {{ }} and {!! !!} in laravel blade files?

Extra strange segment in a form action URL automatically added by laravel

I have this form:
{!!Form::open(['route'=>'fastsearch.show'])!!}
In routes.php I have:
Route::resource('fastsearch','SearchController');
And in SearchController i have a method show() that sends the return to a view called fastsearch (which is fastsearch.blade.php)
If I look into the source of the page where the form is, I see this:
<form method="POST" action="http://localhost:8000/fastsearch/%7Bfastsearch%7D" accept-charset="UTF-8"><input name="_token" type="hidden" value="hLcSkGk2p5XfTkFEv2pwGgcVQB18vHQIGMpOVGpM">
If I put some data in the form and click Submit, I get this error:
MethodNotAllowedHttpException in RouteCollection.php line 201:
My question is why the extra segment in the action URL (this one: /%7Bfastsearch%7D). Is something wrong with the routes ?
(Just to give you all the details, this a general search form that lies on almost every page in order to allow the users to run a quick search from almost every page they might be at that time. So it doesn’t matter if you’re on Home page or on /Home/Subpage/SubSubPage{wildcard}{wildcard} you can still see the form and use it)
You're trying to send a post request to a route expecting a get request.
Change:
{!! Form::open(['route'=>'fastsearch.show']) !!}
To:
{!! Form::open(['route'=>'fastsearch.index']) !!}
Where index is the name of the action you wish to receive the post request.
You're probably better off using specific named routes for this though.
Route::post('fastsearch', [
'as' => 'fastsearch.search', 'uses' => 'SearchController#search'
]);
Take a look at http://laravel.com/docs/5.1/controllers#restful-resource-controllers for more info on resource controllers and http://laravel.com/docs/5.1/routing#named-routes for more on named routes.
You can also use ./artisan route:list for a list of existing routes.

Laravel NotFoundHttpException on route

I have been struggling with something very simple here nevertheless the vagueness around Laravel's routing system that complicates its easy routing approach. I have gone through questions listed here but nothing seems to help me so here it goes.
I have formerly defined a route to my controller on an action named "create". This action is suppose to accept a post data from the form and persist it. This create method has one parameter which defaults to null for a project id if its add else we pass an id e.g domain/projects/add/22 to edit and domain/projects/add to create a new one.
Below is the skeleton of the function:
public function create( $id = null ){ ... }
I then defined a route for this which is:
Route::post( 'projects/add', 'ProjectsController#create' );
Inside my form I have {{ Form::open(array('url' => 'projects/add', 'method' => 'post')) }} .
I keep on getting errors related to routing, Http or method not found exceptions. I tried to follow every suggestion on the net but cannot for the life of me find my way.
Please help me point to the right direction, thanks.
try with below sample code
routs.php►
Route::post('projects/add/{id?}',array('as'=>'project_create','uses'=>'ProjectsController#create'));
ProjectsController.php►
class ProjectsController extends BaseController{
public function create( $id = null ){
...
}
projects.blade.php► (for used blade templating your views should have blade.php extention )
<html>....
<form action="{{ route('project_create') }}"method="post">
....
</form>
</html>
Thank you guys for all your responses. After being swamped with work I finally came back to my project and wanted to try out some of your suggestions but I failed.
I did find a tutorial that I tried to follow and basically was able to have my routes working.
Inside the routes I added (see below):
Route::get('/projects/list', [
'as' => 'post.list',
'uses' => 'ProjectsController#listProjects'
]);
Inside my controller I just created a function that is named listProjects(). As for getting my form displayed I followed the same pattern except point to newProject() method in my controller.
As much as I was not keen on this approach I ended up creating another function just for saving my POSTed form data after a new project form has been filled out and submitted. I still used the same url as projects/add except pointing it to a different function in may controller named saveProject().
About the view I just added the as part of the same save route and it worked. below is a link to the tutorial I followed and taking a look at the code.
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/

Laravel 4 Form::open set action

I'm currently trying out Laravel 4 and I have created a resource controller. In the 'edit' function I'm building a form, which should post to the 'update' function.
To create the form open tag I use the Form::open() function which recently got added to Laravel 4 it seems.
But when I just do Form::open() the action of the form is the current url and I can't figure out how to change the action.
I tried Form::open('clients/' . $client->id) but this gives me the following error:
ErrorException: Catchable Fatal Error: Argument 1 passed to Illuminate\Html\FormBuilder::open() must be of the type array
So I tried Form::open('[clients/' . $client->id). This doesn't generate an error, but now the form open tag is:
<form method="POST" action="http://boekhouding.dev/clients/1/edit" accept-charset="UTF-8" clients/1="clients/1">
And I also tried it like this: Form::open(['action' => 'clients/' . $client->id]) but when I do it like this, the form open tag has no action at all.
So, does anyone know how to set the form action? Using a named route would be perfect, but even being able to set the action at all would be nice.
You can use named route, controller action or simple url to set form action.
To set it via named route use:
{{ Form::open(array('route' => array('route_name', $client->id))) }}
To set it via controller action use:
{{ Form::open(array('action' => array('ClientController#update', $client->id))) }}
So the keyword action does not reffer to 'action' parameter of form tag, but to controller action
And you can also use plain URL like this:
{{ Form::open(array('url' => 'someurl')) }}
#jeffrey_way tweeted about improving the new FormBuilder in Laravel 4. The following paste bucket link should help. It seems to be more about RESTful controllers, but relavant.
Form action sensible defaults - paste bucket
I thought I read something about him coming out with a Forms tutorial tomorrow. If so, it might be found here net.tutsplus.com/?s=laravel

Categories