Laravel blade creating url - laravel

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?

Try this:
{{ url('website/' . $website->name) }}

This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

Related

How to use #{{ }} without escaping the braces in blade?

I have my routes for user profiles like so example.com/#username. So now I want to say something like this:
Go to {{ $user->name }} profile
But this actually escapes the {{ $user->name }} in href attribute (as said in the documentation), so this will literally redirect me to example.com/{{ $user->name }}.
Of course, I can use the pure php way like #<?= $user->name; ?> or any other way. But I want to use laravel's curly braces {{ }}.
Is it possible?
Try this
Go to {{ $user->name }} profile
The only way to work this problem around is to use the above answer. It will make use of blade's {{ }}.
However, I recommend another (semi) approach. You can create a method for the User model (in my case) like so:
class User {
public function handle()
{
return '#' . $this->username;
}
}
Then you can use:
Go to {{ $user->name }} profile
As I said, this is not a general solution. But it works for my case. If you want more general solution, refer to the above answer.

Is there a way to remove the %20 in Laravel URL?

Here's what I have so far.
in blade template
<a href='{{ url("/businessprofile/$business->id/$business->name") }}'>
and in web.php
Route::get('/businessprofile/{id}/{name}', 'BusinessController#show')
it shows
localhost:8000/businessprofile/User%20Info
is there a way to remove the %20 and just show localhost:8000/businessprofile/UserInfo instead?
The Str::slug method generates a URL friendly "slug" from the given string :
{{ url("/businessprofile/$business->id"."/" . Str::slug($business->name)) }}'>}}
Or,
{{ url("/businessprofile/$business->id"."/" . str_slug($business->name)) }}
If above method not work, then change your route & view as :
route :
Route::get('/businessprofile/{id}/{name}', 'BusinessController#show')->name('businessprofile.show');
view :
{{ route('businessprofile.show', ['id' => $business->id, 'name' => str_slug($business->name) ]) }}
See official documentation here
No, you should not do that. That's url encoding and ‰20 is code for space ( ).
change your url as below.
<a href="{{ url('businessprofile/'.$business->id.'/'.$business->name) }}">
Thank you for sharing your answers. I've tried mixing everyone's answer but the best I got is this
<a href='{{ url("/businessprofile/$business->id"."/".Str::slug($business->name)) }}'>
and it shows
http://localhost:8000/businessprofile/1/user-info

Laravel default auth module translation

I have generated the default Laravel auth module.
Everywhere in the blades of the module, I see Double Underscore __ function assuming that translation is almost there.
for example
<li>
<a class="nav-link" href="{{ route('login') }}">
{{ __('Login') }}
</a>
</li>
My question: Where is the translation file? Where should I put it, if I create one?
I mean, if I go to the Laravel documentation site there are examples like this
echo __('messages.welcome');
with explanations
For example, let's retrieve the welcome translation string from the resources/lang/messages.php language file:
BUT in my example above there is no file name specified. It is only text:
__('Login')
Question is: What is used for the language file if no file specified? Is there any default? Where does it sit? Where was it set?
All the language translation files in Laravel should be stored in PROJECT_DIRECTORY/resources/lang. When you make an Auth with artisan, it automatically creates it. But if you can't find it, then create manually.
(1)
There's a way to using translation strings as keys by the docs. In this method you can create a JSON file in PROJECT_DIRECTORY/resources/lang with the name of your local, for example for Spanish name it es.json or German de.json, it depends on your local name.
Now create a JSON object and put the translations with the string name you used in your blade:
{
"Login": "Welcome to Login Page!",
"Logout": "You are logged out!",
}
Then use the PHP double underscores method to call your translations in blades:
{{ __('Login') }}
(2)
Create a file named auth.php in PROJECT_DIRECTORY/resources/lang directory. then put a simple php array like this on it:
<?php
return [
/*
Translations go here...
*/
];`
Then add your translate strings to it:
<?php
return [
'Login' => 'Welcome to Login Page!',
'Logout' => 'You are logged out!',
];`
Now in the blade template simply do this:
<li>
<a class="nav-link" href="{{ route('login') }}">
{{ __('auth.Login') }}
</a>
</li>
Laravel Docs
Have an instruction about the json file. Yes it is not php, but json file. Example would be:
resources/lang/es.json
content
{
"I love programming.": "Me encanta programar."
}
Usage
echo __('I love programming.');
It looks like there are no translation file for the default __('Login'), __('Register'), ... provided by laravel.
By default if no translation is found for __('foobar'), laravel just uses the string in the parentheses. So here, assuming there is no translation file, __('Login') is expanded to 'Login'.

How to the domain url in laravel5

I have to get the domain url (ex. http://www.example.com/) in laravel blade. I've tried using {{ url() }} but it returns the path to my public directory. Is there any one line function to get this? How do I get the domain in blade? Need help. Thanks.
You can also try
{{ Request::server ("SERVER_NAME") }}
{{ Request::server ("SERVER_NAME") }}
Or go with
{{ Request::root() }}

Get image name from database in laravel

I have database, with columns image and alttag. I want to use them in laravel blade view. I try something like this:
{{ HTML::image('images/{{ $item->image }}', $alt="{{ $item->alttag }}") }}
But syntax isn't correct. If i just echo image and alttag like this:
<h1>{{ $item->alttag }}</h1>
then they are correct. I wonder what is wrong in my code.
You used blade syntax in a PHP string. Watch the compiled blade templates to see where you did go wrong.
In short. Try this:
{{ HTML::image('images/'. $item->image, $alt = $item->alttag) }}
or equally short:
{{ HTML::image("images/{$item->image}", $alt = $item->alttag) }}

Resources