I need to get the route in my view for redirecting.
Right now I'm doing this:
Laravel 4 - Get Current Route Name on Hidden Input to use for search
{{ Form::hidden('route', Route::current()->getUri()) }}
Problem is, it looks like this when I get on a page with an id:
<input name="route" type="hidden" value="recipes/details/{id}">
How can I parse the {id} variable?
You should use:
Request::url();
Instead of Route::current()->getUri(), but it's not proper way to redirect from the View, you should redirect from your Controller instead.
It should be in your case (for full url):
// 'http://example.com/recipes/details/10'
{{ Form::hidden('route', Request::url()) }}
or use this (only for path):
// 'recipes/details/10'
{{ Form::hidden('route', Request::path()) }}
Related
I don't want to show /route_name/{id} in the URL field of my Laravel project. Instead of that I want to show /route_name/{name} and also pass the id in the back-end to the controller.
Suppose I have a view department.blade.php. Now click on knee pain from the menubar for seeing the details. Here is my view code
<a href="{{ url('/home_services_under_department/'.$data->id) }}>
{{$data->name}}
</a>"
After click the knee pain a route called. Here is the route
Route::get('/home_services_under_department/{id}', 'frontendDepartmentController#home_services_under_department');
Here is the home_services_under_department method format
public function home_services_under_department($id){}
Here is my URL in browser
http://url/home_services_under_department/2
But I don't want to see the id after home_services_under_department, I want to see the name knee-pain after home_services_under_department as a parameter like
http://url/home_services_under_department/knee-pain
and also want to pass the id to the controller method.
Is it possible in Laravel ?
You can pass the name and can retrieve ID from the database.
1. href
<a href="{{ url('/home_services_under_department/'.$data->name) }}>
{{$data->name}}
</a>
2. route
Route::get('/home_services_under_department/{name}',
'frontendDepartmentController#home_services_under_department');
3. function
public function home_services_under_department($name){
$data= Model_name::select('id')->where('name', $name)->first();
}
You can first pass name in the url then in the controller retrieve the id using the name and proccess with the id whatever you want to do then again return the url with name so that it wont be visible in the url and in the frontend
If you want to secure more , encrypt/hash the name in url with proper algorithm and retrieve it and process it
You should update your route and view file code like:
Route
Route::get('/home_services_under_department/{id}', 'frontendDepartmentController#home_services_under_department')->name('homeservicedepartment');
department.blade.php
{{$data->name}}
No, you can't.
If you want to "pass" the id, either you put in in the URL (that is a 'GET') or send a 'POST' and pass the id as one of the parameters. Of course, you can't send a post with a simple link , but you can fake a post to look like a link.
<style>
form.link_mimic {display:inline}
form.link_mimic input {display:inline;padding:0;border-width:0;margin:0;background:none;color:blue}
form.link_mimic input:hover {text-decoration:underline}
</style>
<form class="link_mimic" method="post" action="my_url">
<input type="hidden" name="lang" value="blah">
<input type="submit" value="this is a form">
</form>
I have a variable $country_code that is displaying the correct value in one part of my form but not in a different part. Why is this happening?
This is my code:
{{ Form::open(['action' => ['PinVerificationController#valid'],'id'=>'pin_code_form']) }}
//$country_code shows 1
We sent a text message to {{$country_code}} {{$phone_number}}. You should receive it within a few seconds.<br><br>
{{ Form::label('Pin Code', null, ['class' => 'control-label']) }}
{{ Form::hidden('country_code', $country_code) }}//<------shows 1-US instead of 1
{{ Form::hidden('phone_number', $phone_number) }}
{{ Form::hidden('type', $pin_notification_type) }}
{{ Form::text('pin_code', null,['placeholder' => 'Pin Code'])}}<br><br>
Enter a 4 digit pin you received by phone.
<br>
<br>
{{ Form::submit('Verify',['name'=>'validate'])}}
{{ Form::close() }}
So if I set $country_code to "1" in my controller it'll display We sent a text message to 1 5555555. You should receive it within a few seconds.
But if I do an inspect element on my hidden form it displays 1-US. I've tried php artisan view:clear and php artisan clear-compiled but the problem still persists.
I've also tried hardcoding a value {{ Form::hidden('country_code', 'asdf') }} and i'm not seeing the change. I tried adding a test {{ Form::hidden('country_code1', 'asdf') }} and see the update.
I also renamed country_code to country_code111 for my hidden field and it displayed the correct value of 1. I thought it was a caching issue but like I mentioned I've tried php artisan cache:clear and the problem is still there.
Since you are using Laravel 5.4, I assume you are using Form from the LaravelCollective, since they were removed from baseline Laravel in 5.x.
LaravelCollective Forms will override the value you provide to the input if it exists in the request data, or in old posted data (the old() function). I suspect this is the case for you.
You can see this behavior implementation here.
To solve this problem, you have a few options:
change the name of the request parameter feeding into the page (if you have control over it)
rename your field name to something that doesn't conflict
Don't use Form:: to generate the form and just use classic html/Blade to create the hidden input automatically
Personally, I would recommend #3 because then you have full control over your code.
<input type="hidden" name="country_code" value="{{ $country_code }}"/>
I want to ask if it is possible to do something like this in View in Laravel 5.2:
<p> This is window: {{$element_ + 'window'}} </p>
<p> This is wall: {{$element_ + 'wall'}} </p>
The values for this variables are from $element_window, $element_wall.
There are couple of options.
First - is to use #php block in .blade file for dynamic output:
#php
${'window'} = ${$element_.'window'}
#endphp
Second is to write custom blade extension to output anything you need.
Third is to define custom method in your Model (if you use one).
However I should mention, that such variable assignment inside template (first option) is not recommended. It's hardly readable and could cause Exceptions if such dynamically created variables do not exist at some point. Not saying that this is not presentation logic.
If you want to dynamically name a variable.. you can do the following.
<p> This is window: {{ ${'element_'.'window'} }} </p>
<p> This is wall: {{ ${'element_'.'wall'} }} </p>
That should work.
But if just want to concatenate a string to the variable... you can use "." :-)
I am trying to setup Angular with Laravel 5.
I have tried doing in appServiceProvider:
public function boot()
{
\Blade::setRawTags("[[", "]]");
\Blade::setContentTags('<%', '%>'); // for variables and all things Blade
\Blade::setEscapedContentTags('<%%', '%%>'); // for escaped data
}
With:
<div>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<h1>Hello, {{ yourName }}!</h1>
</div>
But I'm getting:
Use of undefined constant yourName - assumed 'yourName'...
The easiest way to do this is to simply use # in front of your Angular code:
<div>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<h1>Hello, #{{ yourName }}!</h1>
</div>
source
When doing changes that have to do with Blade (Extending Blade, changing the tags etc) always make sure to delete the cached views.
They're located in storage/framework/views.
Just delete all files (except the .gitignore)
If you want something a bit more practical you can create a command to do that. Like this one
Laravel >= 5.3
While adding # in front of the braces does still work, Laravel has also included another handy blade utility that lets you mark an entire block as-is:
#verbatim
<div>
{{ ctl.variable1 }}
{{ ctl.variable2 }}
</div>
#endverbatim
Particularly helpful if you're not doing much with the Blade template rendering
Else you modify angular syntax...
var sampleApp = angular.module('sampleApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
I have this in template
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}
The form is appearing as
fos_user_registration_form_username --input box
fos_user_registration_form_email
fos_user_registration_form_plainPassword_first
fos_user_registration_form_plainPassword_second
But it want to have simple labels like Username , Email etc. How to do that
You can parse the individual form fields instead of the form as a whole.
{{ form_widget(form.fos_user_registration_form_username) }}
In this way, you can parse a single form element. Make sure to end with
{{ form_rest(form) }}
to output any not yet parsed fields (such as the csrf protection token).
using the above approach you can add your own labels to the fields.
FOSUserBundle uses the translator system. You need to as seen in the docs here:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/index.md
Add this:
# app/config/config.yml
framework:
translator: ~
To your config file (app/config/config.yml).
This will tell symfony to replace all the label values with the values found in the translator file (FOSUserBundle.en.yml). Then the form will always print "Username" instead of "fos_user_registration_form_username".