Laravel form method get - url parameters - php

I got a form in laravel with Method='GET'
{{ Form::open(array('method' => 'get')) }}
I got a few input fields in there: city, skillLevel, province and category.
When i submit the form i get a awfull url like this:
http://localhost/vacatures?city=somehwhere&skillLevel=junior&province=Zeeland&category=django
but i want something like this:
http://localhost/vacatures/somewhere/junior/Zeeland/django
How do i achieve this? I've tried giving at a route attribute and a action actribute, but that didnt work out.

Maybe this topic will help you - How To Pass GET Parameters To Laravel From With GET Method ?
There are 2 nice answers, which should suit to your problem.

Related

Laravel retrieve id from URL

I would like to retrieve an id from a Laravel URL.
I've tried this:
$request->route('id');
But it would tell me that $request is undefined.
The url I am using is:
http://192.168.1.14/public/tasks/create?id=1
I've got a sidebar and within that I've got an if statement.
#if((request()->is('projects/1')) || (request()->is('tasks/create')))
<a href="{{ route('tasks.create') }}?id={{ $project->id }}">
#endif
I've hid the unnecessary code, but the second parameter is used for highlighting what page I am on in the sidebar. And the first for showing the create task option when I am on a project page.
Now, the if statement causes that its hidden ( so I can't see the error ( the error would say that there is no id but its true because it only receives the id by being on a project page with an id ))
Now, the task form on the page itself has a hidden input field with project_id so I can bind the task to the project.
Inside the value I have the $request->route('id') but it gives me an error that request is undefined.
Now I ask, how do I retrieve the id from the url, or else, can I retrieve the id another way? All the project and task blade files extend the project.index and the project.index extends the layouts.blade so its a layout within a layout.
Also, can I change the (request()->is('projects/1')) to something else so whatever project page I am on, the statement is true? For example 'projects/{project}'
Thanks for any help
Best way to do this is
You don't need to write 'create'
{{ app('request')->input('id') }}
or
{{ request()->get('id') }}
IF you are not sure how it works just
store in a variable and print it on view to cross-verify like this.
<?php
$id = app('request')->input('id');
//or
$id = request()->get('id');
?>
You can always check all request URL parameters like this.
$request = request();
print_r($request);
Create Route for getting id from url
Example:
Route::name('subscriptions.store')->post('/subscriptions/store/{id}', 'CreateSubscriptionController');

Updating database ids, what to do with form url

Updating existing items in DB in my application is solved by this way:
I have url like this:
http://localhost/project/public//structure/edit/about-us
In router I have set
Route::get('/structure/edit/{url}', 'StructureController#update'); //for displaying the prefilled form
Route::post('/structure/edit/{url}', 'StructureController#update'); // for saving new values
So it means, I'm building update query where url = $url . This is the main part of my view file:
{!! Form::open(['url' => URL::current()]) !!}
I don't know where to point "form action". So I¨m using the same url as current url, so router recognizes, that this is post request and I can process the update inside the same controller and select new (updated) data to my update form.
The problem is, when I update the url via form, new value will set to database. So it means, from this moment old url doesn't exists, but and my form action point to url, which doesn't exists anymore.
What can I do with that? If you know, what I mean...
To update use patch method instead of post. Write this in web.php
Route::get('/structure/edit/{id}', 'StructureController#edit');
Route::patch('/structure/{id}/update', 'StructureController#update');
You can use either action or url as a form action. Pass the structure id in the second parameter of the action
{!! Form::model($structure,['action' => ['StructionController#update',$structure->id],'method'=>'patch']) !!}
If you do what #smartrahat suggests and still got the error you posted, then can you run php artisan route:list command and show us the structure of your routes?

Twig variable inside other twig variable?

So, I've been working on a website for a reggae backing band and on the 'booking' page I have a form. After submitting the form I first (ofcourse) check if all required fields are filled in, if something else went wrong or if it succeeded.
But, I have a couple of language files (nl, de, fr and en). On the HTML page I first check if an error was set inside the PHP and if there is an error, I want to output it in the right language.
So in case of an error, the HTML would have to look like this
{{ lang.booking.{{ form_error }} }}
because I am sending the error type in the PHP as well, but this doesn't seem to be possible (obviously).
Can someone help me out or tell me how to get around this problem?
Thanks in advance!
You can use the attribute function to access a dynamic attribute of a variable.
{{ attribute(lang.booking, form_error) }}
You can use translation on constraint messages as you can learn in this article: http://symfony.com/doc/current/validation/translations.html

Laravel 5 MethodNotAllowedHttpException PUT

I am trying to update a user, but when I hit the submit button, Laravel throws the below error:
"RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 206".
I think that the PUT method is not allowed, but I do not understand the reason. The request never reaches UserController#update.
I have configured a resource route like this:
Route::resource('backend/users', 'Backend\UsersController');
The output of php artisan route:list is:
I solved the problem like this: it must be the form's post action error;
<form method="POST" action="10.241.229.1/backend/users/{{$user->id}}"; accept-charset="UTF-8">
add the id you want update to the action.
use put method like this within form,for more https://laravel.com/docs/5.2/routing#form-method-spoofing
{{ method_field('PUT') }}
Coming a bit late on this question.
In my experience this kind of error comes for two reasons:
as Laravel docs say, HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form.
if you are making the request from a HTML form, and you have the VerifyCsrfToken middleware enable, than you will need to add a hidden _token field to the form with {{ csrf_token() }} as value.

Issue with Laravel 4 Routes?

I'm trying to learn laravel, but I'm running into an issue with routing.
I have the following in my Routes file:
Route::get('home', function()
{
return View::make('home');
});
Which works if I type
http://localhost/laravel/public/home
However, on a another page I have a form, that when submitted should take me to that page like so:
{{ Form::open(array('url' => 'home')) }}
Now this takes me to the correct address, but throws an exception. But, if I reload the page with the same URL then the page loads correctly. So what is the issue here? Is there a problem with the way my form is set up?
Route::get is when you want to issue a get request to the page, to get the contents basically. If you want to post to a page, you need to do, in addition to your Route::get,
Route::post
Another option that people will tell you is:
Route::any
but I would advise staying away from this because the logic will probably be different for the two routes.

Categories