ran4
ran4

Reputation: 29

Undefined variable: category in edit.blade

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) Undefined variable: category (View: W:\domains\blog\resources\views\admin\categories\edit.blade.php) Previous exceptions

I do not understand what the error checked everything a few times ['categories.update',$category->id], 'method'=>'put'])); ?>

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

Upvotes: 0

Views: 448

Answers (3)

Masoud.Najafi
Masoud.Najafi

Reputation: 9

it's simple as you know!

just replace in your edit function

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

with:

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

Upvotes: 0

Sethu
Sethu

Reputation: 1379

It happens because you've misplaced the ', you should pass the value correctly in return

replace your edit method with this:

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

Upvotes: 1

brnd0
brnd0

Reputation: 442

When posting code try to make it as readable as possible. Easiest way to accomplish that is to read the WHOLE post before you post it.

On the issue:

In the CategoriesController@edit reaplace

 ['category=>$category']

with

 ['category'=>$category]

Also in this scenario you can use the

compact('category') 

instead of the array as the variable $category and the key in the array 'category' have same name.

Upvotes: 0

Related Questions