I'm trying to set active class in my list item, but it doesn't work.

我在blade中的代码:

@foreach($data as $site)
  <ul class="sidebar-menu" id="second-menu">
  @if(isAdmin())<li class="{{-- active class  for url parameter --}}"><a href="{{ url('sites/'.$site->id.'/edit') }}" >{{ $site->name }}</a></li>
@endif
</ul>
@endforeach

So, if I write: li class="@if(getRouteName() == 'site@index'){{ 'active' }}@endif" , it works nice, but in my case the problem is that I want to get 'active' class in foreach sites/'.$site->id.'/edit

非常感谢.

推荐答案

Use is() method. For example:

<li class="{{ request()->is('sites/*/edit') ? 'active' : '' }}"

Laravel相关问答推荐

Nginx上带有SSL的Laravel混响

为什么Laravel在API请求时返回BadMethodCallException?

如何在 Laravel 中使用 return 停止 Trait php 的执行

Laravel 语法 GroupBy 获取列到数组

在 Laravel 中清除 Routes&config:cache 后未定义的常量

如何保存/重定向 Laravel Artisan 命令的输出?

在 Laravel 中下载后如何重定向?

向 Docker 上的 Artisan 推荐方式

如何更改默认 Laravel Auth 登录视图

Laravel Blade - 如果满足条件,则添加一个类

在 Laravel 测试用例中模拟一个 http 请求并解析路由参数

调用未定义的方法 Illuminate\Routing\Route::get()

在 Laravel 的数据透视表中添加 id 列有什么好处?

Laravel php artisan 产生错误

使用 Nginx 设置 Laravel

如何在特定项目中禁用初始化 JS/TS 语言功能?

如何判断是否连接到 Laravel 4 中的数据库?

Laravel 排序集合,然后按键

Laravel 更新查询

为什么`catch (Exception $e)` 不处理这个`ErrorException`?