【问题标题】:Undefined variable "category" in foreach loop laravel 4.2foreach循环laravel 4.2中未定义的变量“类别”
【发布时间】:2014-07-28 01:01:27
【问题描述】:

我的路线:

Route::controller('admin' , 'CategoriesController');

控制器:

<?php

class CategoriesController extends BaseController {

    /**
     * Display a listing of the resource.
     * GET /categories
     *
     * @return Response
     */

    public function __construct() {
        $this->beforeFilter('csrf', array('on' =>'post' ));
    }

    public function getIndex()
    {
        return View::make('categories.index')
        ->with('categories', Category::all());
    }

    /**
     * Show the form for creating a new resource.
     * GET /categories/create
     *
     * @return Response
     */
    public function postCreate()
    {
        $validator = Validator::make(Input::all(), Category::$rules);

        if ($validator->passes()) {
            $category = new Category;
            $category->name = Input::get('name');
            $category->save();

            return Redirect::to('admin/categories/index')
            ->with('message' , 'Category created');
        }

        return Redirect::to('admin/categories/index')
        ->with('message' , 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /categories/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function postDestroy()
    {
        $category = Category::find(Input::get('id'));

        if ($category){
            $category->delete();
            return Redirect::to('admin/categories/index')
            ->with('message' , 'category deleted');
        }
        return Redirect::to('admin/categories/index')
            ->with('message' , 'something went wronng');
    }

}

型号:

<?php

class Category extends \Eloquent {
    protected $fillable = array('name');

    public static $rules = array('name'=>'required|min:3');
}

查看/index.blade:

@section('content')
<div id="admin">
    <h1>Categories Admin panel</h1>
    <p>Here you can view, delete, and create new categories.</p>
    <h2>Categories</h2>

    <ul>
    @foreach($categories as $category)
        <li>
        {{ $category->name }} 
        {{ Form::open(array('url'=>'admin/categories/destroy', 'class' => 'form-inline'))}}
        {{ Form::hidden('id' , $category->id) }}
        {{ Form::submit('delete')}}
        {{ Form::close();}}
        </li>
    @endforeach
    </ul>

    <h2>Create New Category</h2>
    @if($errors->has)

        <div id="form-errors">
            <p>The following erros</p>
            <ul>
            @foreach($errors->all() as $error)
                <li>{{ $errors }}</li>
            @endforeach
            </ul>
        </div><!--end form-errors-->
    @endif

    {{ Form::open(array('url'=>'admin/categories/create'))}}
    <p>
    {{ Form::label('name')}}
    {{ Form::text('name')}}
    </p>
    {{ Form::submit('Create Category', array('class' => 'secodary-cart-btn'))}}
    {{ Form::close()}}
</div><!--end admin-->
@stop 

问题是:当我在浏览器中运行此代码时,它显示undefined variable: category in index.blade.php

【问题讨论】:

  • 你有堆栈跟踪吗?你运行哪个控制器方法?

标签: php foreach laravel-4


【解决方案1】:

请在浏览器中检查您尝试访问的路线。

问题是,您正在尝试使用 RESTful 控制器。在这种情况下,您必须在 restful 方法中访问 URL。

如果你想在你的restful控制器中访问getIndex()方法,那么在你的浏览器中,你必须访问

http://www.yoursite.com/admin/index

基本上,Route::controller('admin','CategoriesController');意味着,当您的 URL 中有 /admin/ 时,它将访问此 CategoriesController。然后将使用 URL 路径后跟的动词。

如果你想走非 RESTful 路线,那么按照以下方式之一进行

像这样在你的路由中传递 $categories 数组

Route::get('/', function(){
  return View::make('categories.index')->with('categories',Category::all());
});

Route::get('/',array('uses'=>'CategoriesController@getIndex'));

然后在您的 CategoriesController 中

public function getIndex(){
        return View::make('categories.index')
        ->with('categories', Category::all());
}

应该修复它:)

【讨论】:

    猜你喜欢
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2020-02-28
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2020-03-03
    相关资源
    最近更新 更多