I have this form in blade:
{{ Form::open(['action' => ['SearchController#searchUser'], 'method' => 'GET']) }}
{{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter name'])}}
{{ Form::submit('Search', array('class' => 'button expand')) }}
{{ Form::close() }}
How can I translate it into html?
If you're using Laravel 5.2, you must install HTML and Form as module.
follow these steps and it must be rendered your code:
https://laravelcollective.com/docs/5.2/html
If you are new to Laravel and feel its confusing with all those syntax you can use normal HTML that you already know. Its not a compulsion to use Laravel's blade shortcut.So something like
<form action="SearchController#searchUser" method="get">
//All your inputs
</form>
And to use form in your project you have to get a facade. You can follow the tutorial below :
http://www.easylaravelbook.com/blog/2015/02/09/creating-a-contact-form-in-laravel-5-using-the-form-request-feature/
Related
I have a reject function in my Calendar controller but whenever I redirect to the view page it displays an error saying my route is not defined.
I've tried rearranging and renaming my route but it's still displaying the error.
Here is my form:
{!! Form::open(['url' => route('therapist.reject.appointment', $bookingRequest), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
{{csrf_field()}}
{!! Form::close() !!}
Here are my routes. The other routes displayed are working perfectly:
Route::get('therapist-calendar/{bookingRequest}', 'TherapistCalander')->name('therapist.calendar');
Route::post('therapist-calendar/{bookingRequest}',
'TherapistCalander#saveAppointment')->name('therapist.book.appointment');
Route::patch('therapist-calendar/{bookingRequest}',
'TherapistCalander#finishedAppointment')->name('therapist.finish.appointment');
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#cancelAppointment')->name('therapist.cancel.appointment');
And lastly, my function:
public function rejectAppointment(Request $request, BookingRequest $bookingRequest)
{
$bookingRequest->reject();
return redirect()->back()->with('rejectStatus', true);
}
The view page where this button belongs should be able to display the buttons for rejecting and finishing, alongside the calendar view.
EDIT
Follow up question: Is it possibly because the routes are similar to one another? If so, how do I fix this?
Try to change the Reject and Cancel the url string because it is similar.
Route::delete(
'therapist-calendar/{bookingRequest}/delete',
'TherapistCalander#rejectAppointment'
)->name('therapist.reject.appointment');
Route::delete(
'therapist-calendar/{bookingRequest}',
'TherapistCalander#cancelAppointment'
)->name('therapist.cancel.appointment');
Change your code to
{!! Form::open(['url' => route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]), 'method' => 'delete', 'onsubmit' => 'javascript:return confirm("Are you sure?")']) !!}
{{csrf_field()}}
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
{!! Form::close() !!}
The route parameters are passed as an array and that should work fine. Refer doc
Can you try this code
<form action="{{ route('therapist.reject.appointment', ['bookingRequest' => $bookingRequest]) }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-warning btn-block">Reject this appointment</button>
</form>
UPDATE
PROBLEM FIXED
I realized since they have similar links, web.php found it confusing so it did not read this route.
That is why I changed my route from:
Route::delete('therapist-calendar/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');
To this:
Route::delete('doReject/{bookingRequest}',
'TherapistCalander#rejectAppointment')->name('therapist.reject.appointment');
I have a web form in Laravel that is behind an ip. Example:
127.0.0.1/posts/create
With a simple form helper:
{{ Form::open(array('url' => '/posts','files'=>'true')) }}
#include('posts._form')
{!! Form::submit('Create Post', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
When I inspect the html, https is added to the url. How can I stop this?
Found the problem, my nginx had the HTTPS variable set on. Laravel apparently reads that variables and adds the protocol + host_name to all redirect actions.
i want to submit something like this
<form action="submit#myresults">
I have read here:
https://laravel.io/forum/02-07-2014-how-to-append-hashtag-to-end-of-url-with-redirect
so this won't work:
{!! Form::open(['route' => 'my_fooroute']) !!}
what is the cleanest way?
this seems to be the cleanest form:
{!! Form::open(['url' => route('my_fooroute') . '#bar'] !!}
You can do something like:
Route::get('/blog#{hash_tag?}', 'Controller#Method')->name('test');
Then, you open form like:
{!! Form::open(['route' => ['test', 'hash_you_want']]) !!}
Or, URL like:
My Link
I use Laravel 5 and try to update a form:
{!! Form::model($user, ['route' => ['edit', $user->id], 'method' => 'PUT']) !!}
{!! Form::label('titel', 'First Name:'!!}
{!! Form::text('titel', null,) !!}
<button type="submit">Update</button>
{!! Form::close() !!}
My route:
Route::post('edit/{id}', ['as' => 'edit', 'uses' => 'UserController#editUser']);
My controller:
public function editUser($id){};
When click on the update Button I get MethodNotAllowedHttpException in RouteCollection.php
I checked in the browser source code and saw that Form::model(..) which I use generate the following output:
<form method="POST" action="http://localhost/myProject/public/edit/1" accept-charset="UTF-8"><input name="_method" type="hidden" value="PUT"><input name="_token" type="hidden" value="4nZlyfzzAZmTcZfThQ8gcR6cgEgYgR0ip0JZTKck">
Within the form there is the attribute method="POST" and the hidden input has the attribute value="PUT". This seems not correct for me. Any ideas? Thank you
You should use 'update' route to actually save the data (validate and persist it to the database). 'edit' route is what you used to generate edit form.
You should use PUT method to run method which saves data.
Also, here is small tip for you. Learn about how RESTful controllers work. They are really easy way to do what you're doing here (defenetly worth to learn them):
https://laravel.com/docs/5.1/controllers
Your route is not the same as your form.
Laravel uses the hidden inputs to specify the different http methods, as put.
So in your routes you should use a put method not a post.
Route::put();
1st question:
I've inserted the localization in many types of texts and things, but I don't know how to import it into in the following forms:
{{ Form::label('name', 'here') }}
{{ Form::text('name', null, array('placeholder' => 'here')) }}
{{ Form::submit('here', array('class' => 'btn btn-success')) }}
{{ Form::button('here', array('class' => 'btn btn-default')) }}
I want it to be in the form label 'here' and in the placeholder of the text 'here'.
2nd question:
I am not allowed to insert it with links in my language file: text here blah blah BLAH?
Is there anyway to insert it with links?
Thanks in advance.
Supposing that your messages are stored in app/lang/en/message.php you can use the same way for all your cases:
In Blade template:
{{ Form::label('name', Lang::get('message.key')) }}
{{ Form::text('name', null, array('placeholder' => Lang::get('message.key'))) }}
{{ Form::submit(Lang::get('message.key'), array('class' => 'btn btn-success')) }}
In HTML tag mixed with some Blade expression:
BLAH
You can also use localization in Blade templates using strictly Blade syntax.
Given that you have a message in your /app/lang/en/messages.php that corresponds to the key "message_key", you can do :
#lang('messages.message_key')
to render the message in the locale that your application is configured to use.
So, The answers for both of your questions are:-
1) {{ Form::label('name', 'here') }}
Here, you need to change the "here" text hence laravel localization method can be used.For eg:-
{{ Form::label('name', '__("Here")' }} or
{{ Form::label('name', '__('message.here') }}.
2)< a href="{{ URL::to('text') }}">BLAH< /a>
Here, you need to change the label instead of link.
< a href="URL::to('text') ">{{__('message.BLAH')}}< /a>.
This is the simplest which works for me !
{!! Form::label('title', trans('users.addNewRecordsNameFieldLabel'),['class' => 'control-label']) !!}
I feel, already blade parsing is started the moment {!! or {{ is started
A possible answer to your second question:
You could set up a language file resources/lang/en/page.php like this:
return [
'sentence' => 'A sentence with a :link in the middle.',
'link_text' => 'link to a another page'
];
And use it in a Blade template like this:
{!! trans('page.sentence', [
'link' => '' . trans('page.link_text') . ''
]) !!}}
The result would be:
A sentence with a link to a another page in the middle.
You alse can use __() method
{{ __('app.name') }}