Undefined variable: title (View: C:\xampp\htdocs\myproject\resources\views\categories\index.blade.php)

时间:2019-04-08 12:59:43

标签: laravel model-view-controller laravel-5.4 php-7.0

I try to make a page index.blade, but i getting error

Undefined variable: title (View: C:\xampp\htdocs\myproject\resources\views\categories\index.blade.php)

I am using laravel 5.4 PHP 7.0.33 Is there anything wrong with the code?

My Controller

<?php

namespace App\Http\Controllers;
use App\Category;

use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categories = Category::orderBy('created_at', 'DESC')->paginate(10);
        return view('categories.index', compact('categories'));//
    }

My index.blade this is my view/categories/index.blade.php

@extends('layout.master')
​
@section('title')
    <title>Manajemen Kategori</title>
@endsection
​
@section('content')
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark">Manajemen Kategori</h1>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#">Home</a></li>
                            <li class="breadcrumb-item active">Kategori</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>
​
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-4">
                        @card
                            @slot('title')
                            <div class="card">
                                <div class="card-header with-border">
                                 <h3 class="card-title">{{ $title }}</h3>
                            </div>
                            <div class="card-body">
                                {{ $slot }}
                            </div>
                                {{ $footer }}
                            </div>
                            @endslot

                            @if (session('error'))
                                @alert
                                <div class="alert alert-{{ $type }} alert-dismissible">
                                    {{ $slot }}
                                </div>
                                @endalert
                            @endif
​
                            <form role="form" action="{{ route('kategori.store') }}" method="POST">
                                @csrf
                                <div class="form-group">
                                    <label for="name">Kategori</label>
                                    <input type="text" 
                                    name="name"
                                    class="form-control {{ $errors->has('name') ? 'is-invalid':'' }}" id="name" required>
                                </div>
                                <div class="form-group">
                                    <label for="description">Deskripsi</label>
                                    <textarea name="description" id="description" cols="5" rows="5" class="form-control {{ $errors->has('description') ? 'is-invalid':'' }}"></textarea>
                                </div>
                            @slot('footer')
                                <div class="card-footer">
                                    <button class="btn btn-primary">Simpan</button>
                                </div>
                            </form>
                            @endslot
                        @endcard
                    </div>
                    <div class="col-md-8">
                        @card
                            @slot('title')
                            List Kategori
                            @endslot

                            @if (session('success'))
                                @alert(['type' => 'success'])
                                    {!! session('success') !!}
                                @endalert
                            @endif

                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <td>#</td>
                                            <td>Kategori</td>
                                            <td>Deskripsi</td>
                                            <td>Aksi</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @php $no = 1; @endphp
                                        @forelse ($categories as $row)
                                        <tr>
                                            <td>{{ $no++ }}</td>
                                            <td>{{ $row->name }}</td>
                                            <td>{{ $row->description }}</td>
                                            <td>
                                                <form action="{{ route('kategori.destroy', $row->id) }}" method="POST">
                                                    @csrf
                                                    <input type="hidden" name="_method" value="DELETE">
                                                    <a href="{{ route('kategori.edit', $row->id) }}" class="btn btn-warning btn-sm"><i class="fa fa-edit"></i></a>
                                                    <button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></button>
                                                </form>
                                            </td>
                                        </tr>
                                        @empty
                                        <tr>
                                            <td colspan="4" class="text-center">Tidak ada data</td>
                                        </tr>
                                        @endforelse
                                    </tbody>
                                </table>
                            </div>
                            @slot('footer')
​
                            @endslot
                        @endcard
                    </div>
                </div>
            </div>
        </section>
    </div>
@endsection

My route web.php

Route::resource('/kategori', 'CategoryController', 
                ['except' => ['create', 'show']]);

3 个答案:

答案 0 :(得分:3)

It should be like this

public function index()
{
    $categories = Category::orderBy('created_at', 'DESC')->paginate(10);
    $title = ''; //your title
    return view('categories.index', compact('categories','title'));
}

because title not getting value from controller.

答案 1 :(得分:0)

您没有传递变量标题来查看 添加这样的内容:

$title = 'Your title'; return view('categories.index', compact('categories','title'));

答案 2 :(得分:0)

如果标题是“类别”字段

如果title是类别模型的字段/成员,那么您将执行{{ $category->title }}。我认为这是真正的错误。

否则

您需要像其他人一样定义和发送变量。

$tile='Your Title';
return view('categories.index', compact('categories','title'));