未定义的变量:edit.blade中的类别

时间:2019-05-14 15:22:37

标签: laravel

edit.blade.php

    @extends('admin.layout')

        @section('content')
        <!-- Content Wrapper. Contains page content -->
          <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
              <h1>
                Добавить категорию
                <small>приятные слова..</sma

ll>
          </h1>
        </section>

        <!-- Main content -->
        <section class="content">

          <!-- Default box -->
          <div class="box">
            <div class="box-header with-border">
              <h3 class="box-title">Меняем категорию</h3>
              @include('admin.errors')
            </div>
            <div class="box-body">
            {{Form::open(['route'=>['categories.update',$category->id], 'method'=>'put'])}}
              <div class="col-md-6">
                <div class="form-group">
                  <label for="exampleInputEmail1">Название</label>
                  <input type="text" class="form-control" id="exampleInputEmail1" name="title" placeholder="" value="{{$category->title}}">
                </div>
            </div>
          </div>
            <!-- /.box-body -->
            <div class="box-footer">
              <button class="btn btn-default">Назад</button>
              <button class="btn btn-warning pull-right">Изменить</button>
            </div>
            <!-- /.box-footer-->
            {{Form::close()}}
          </div>
          <!-- /.box -->

        </section>
        <!-- /.content -->
      </div>
      <!-- /.content-wrapper -->

    @endsection

    <?php

namespace App\Http\Controllers\Admin;
use View;
use App\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoriesController extends Controller
{
  public function index()
    {
        $categories = Category::all();

        return view('admin.categories.index', ['categories' =>  $categories]);
    }
  public function create()
  {
    return view('admin.categories.create');
  }
 public function store(Request $request)
 {
   $this->validate($request, [
            'title' =>  'required' //обязательно
        ]);

   Category::create($request->all());
   return redirect()->route('categories.index');
 }
 public function edit($id)
 {
   $category = Category::find($id);
   return view('admin.categories.edit', ['category=>$category']);
 }
 public function update(Request $request, $id)
 {
   $category = Catefory::find($id);
   $category->update($request->all());
   return redirect()->route('categories.index');
 }
}

CategoryController.php ErrorException(E_ERROR) 未定义的变量:类别(视图:W:\ domains \ blog \ resources \ views \ admin \ categories \ edit.blade.php) 以前的例外情况

我不明白该错误几次检查了所有内容         ['categories.update',$ category-> id],'method'=>'put']))); ?>

      <div class="col-md-6">

3 个答案:

答案 0 :(得分:1)

发生这种情况是因为您放错了',您应该正确地传递值作为回报

为此替换您的编辑方法:

public function edit($id)
{
    $category = Category::find($id);
    return view('admin.categories.edit', ['category' => $category]);
}

答案 1 :(得分:0)

发布代码时,请尝试使其尽可能可读。实现此目的最简单的方法是在发布整个帖子之前先阅读一下。

关于这个问题:

CategoriesController@edit区域中

 ['category=>$category']

 ['category'=>$category]

在这种情况下,您也可以使用

compact('category') 

变量$category与数组'category'的键名相同,而不是数组。

答案 2 :(得分:0)

这很简单!

只需替换您的编辑功能

   return view('admin.categories.edit', ['category=>$category']);

具有:

   return view('admin.categories.edit')->withCategory($category);