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.
Related
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]) }}
In blade, If we want to check that the current route matches with a route or not, we can simply use:
#if(Route::currentRouteName() == 'parameter')
{{ 'yes' }}
#else
{{ 'no' }}
#endif
But what if we want to match it with a wildcard like:
#if(Route::currentRouteName() == 'parameter.*')
{{ 'yes' }}
#else
{{ 'no' }}
#endif
Is there any solution for that?
I have tried "*" and ":any", but it didn't work.
Note: I want to check route, not URL.
Any help would be appreciated.
Thanks,
Parth Vora
Use Laravel's string helper function
str_is('parameter*', Route::currentRouteName())
It'll return true for any string that starts with parameter
I had the same problem. I wanted to toggle an active class based on a URI.
In blade (Laravel 6x), I did:
(request()->is('projects/*')) ? 'active' : ''
You can also make use of Blades Custom If Statements and write something like this in your AppServiceProvider.php:
public function boot()
{
Blade::if('route', function ($route) {
return Str::is($route, Route::currentRouteName());
});
}
then you can use it in a blade view like this:
<li #route('admin.users*') class="active" #endroute>
Users
</li>
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() }}
I have to display history of changes that are made in my app (like update, insert->who did it , what field changed and when). I am using laravel 4 and I also downloaded this. But the problem is that I am not clear of how to use it. Where to put the folder VentureCraft ? and how to get username or id of the person who did the action. Is there any other way to keep track of history in laravel?
EDIT:
Car model
namespace MyApp\Models;
class Car extends Eloquent {
use \Venturecraft\Revisionable\RevisionableTrait;
}
View
#foreach($description->revisionHistory as $history )
<li>{{ $history->userResponsible()->username }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}</li>
#endforeach
and it shows :
Undefined variable: description
Am i missing something in controller?
Check out the README, it looks fairly self explanatory. Install with composer, migrate, then use the RevisionableTrait in your models that you wish to keep the update history of. You can should then be able to get the history of an object with:
$object->revisionHistory;
They even have a simple example of usage within a blade template:
#foreach($account->revisionHistory as $history )
<li>{{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}</li>
#endforeach
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) }}