Php 如何在Laravel中为类别添加特定标题和说明

Php 如何在Laravel中为类别添加特定标题和说明,php,laravel,Php,Laravel,我正在尝试为我基于Laravel的网站中的每个帖子和类别添加特定的标题和元描述 到目前为止,我已经完成了文章的标题和描述,但我也不知道如何对类别进行描述 这是我在帖子中使用的代码,它起作用了: @if(isset($post->title)) <title>{{ $post->title }}</title> @else <title>{{ $settings->website_name }} - {{ $settings->w

我正在尝试为我基于Laravel的网站中的每个帖子和类别添加特定的标题和元描述

到目前为止,我已经完成了文章的标题和描述,但我也不知道如何对类别进行描述

这是我在帖子中使用的代码,它起作用了:

@if(isset($post->title))
  <title>{{ $post->title }}</title>
@else
  <title>{{ $settings->website_name }} - {{ $settings->website_description }}</title>
@endif

@if(isset($post->description))
<meta name="description" content="{{ $post->description }}" />
@else
<meta name="description" content="Example Example Example" />
@endif
以下是我获取类别的方式:

<a href="#" class="dropdown-toggle categories" data-toggle="dropdown"><i class="fa fa-folder-open"></i> {{ Lang::get('category.categories') }} <b class="caret"></b><div class="nav-border-bottom"></div></a>

<ul class="dropdown-menu">
                <li>
                    @foreach ($categories as $category)
                      <a href="{{ URL::to('category') . '/' . slugify($category->name) }}">{{ $category->name }}</a>
                    @endforeach
                </li>
              </ul>

  • @foreach($categories作为$category) @endforeach

您好,如果您使用的是blade,那么很简单。如果您想添加唯一的标题和说明,那么请修改blade布局,如下所示

<title>@yield('meta_title', 'Default Title')</title>
<meta name="description" content="@yield('meta_description', 'Default Description')" />
@section('meta_title')
    Your title text will go here
@stop

@section('meta_description')
    Your description text will go here
@stop

在第一个
@if
中,
($categories->description)
前面缺少
isset
。您的确切错误是什么,与您期望的有什么不同?我在这里发布时手动编写了此代码,在我的页面上代码很好。问题是,当我为类别添加此代码时,我会看到一个空白页。请检查您的登录
app/storage/logs/laravel.log
,或者检查您的服务器
error.log
,查看您遇到的错误。也许您没有任何错误。荣誉我想我这里的2p是,最好的方法是使用刀片模板继承,而不是布局中的大量
@if
s,将标题/元数据定义为单个视图可以覆盖的部分。这样,您可能不需要执行任何ifs,因为如果您在类别页面上,您知道有一个
$category
变量可以使用。但是这个建议在一定程度上改变了您的体系结构,如果您不喜欢这样做,我理解。哦,这个错误看起来像是说变量未定义-您是否需要将
$categories
重命名为
$categories
?您提到了
categories
是表名,但可能单个实例名是
$categority
?My categories.blade,以以下代码开头:@extends('layouts.master')@section('content')我应该放在@extends('layouts.master')之后吗,在extendsFYI@toofast之后试试这几乎是我上面的建议,你说你不喜欢它。显然,如果你这样做了,一切都会好起来。只是让你知道。(+1,用于将工作转化为答案。)谢谢,但我实际上没有category.blade文件,我使用此代码获取类别-请参阅编辑的主要帖子。
<title>@yield('meta_title', 'Default Title')</title>
<meta name="description" content="@yield('meta_description', 'Default Description')" />
@section('meta_title')
    Your title text will go here
@stop

@section('meta_description')
    Your description text will go here
@stop