致命错误类名必须是有效对象或字符串

时间:2014-09-09 13:49:36

标签: php laravel laravel-4

我是laravel和php的新手,我在我的控制器中有这个功能,它给了我那个错误 致命错误类名称必须是有效对象或字符串

public function getIndex () {
    $categories = array();

    foreach ( $Category::all() as $key=> $category) {
        $categories[$category->id] = $category->name ;
    }

这是我的整个控制器

<?php 

class ProductsController extends BaseController {



public function __construct(){

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

public function getIndex () {
    $categories = array();

    foreach ( $Category::all() as $key=> $category) {
        $categories[$category->id] = $category->name ;
    }


    return View::make('products.index')
    ->with('products' , Product::all())
    ->with('categories' , $categories);
 }

public function postCreate(){

    $validator = Validator::make(Input::all() , Product::$rules);

    if ($validator->passes()){
        $product = new Product ; 
        $product->category_id = Input::get('category_id');
        $product->title = Input::get('title');
        $product->description = Input::get('description');
        $product->price = Input::get('price');

        $image = Input::file('image');
        $filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
        Image::make($image->getRealPath())->resize(468,249)->save('public/img/products/'.$filename);
        $product->image = 'img/products/'.$filename;
        $product->save(); 

        return Redirect::to('admin/products/index')
        ->with('message' , 'Product Created');


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

public function postDestroy(){

        $product = Product::find(Input::get('id'));

        if($product){
            File::delete('public/'.$product->image);
            $product->delete() ;
            return Redirect::to('admin/products/index')
            ->with('message' , 'Product Deleted');

        }
        return Redirect::to('admin/products/index')
            ->with('message' , 'Something went wrong');


     }

    public function postToggleAvailability(){
        $product = Product::find(Input::get('id'));
        if($product){
            $product->availability = Input::get('availability');
            $product->save();
            return Redirect::to('admin/product/index')->with('message', 'product updated');

        }
        return Redirect::to('admin/product/index')->with('message' , 'Invalid Product');

    }


}

1 个答案:

答案 0 :(得分:0)

假设该类名为Category,则应在foreach中使用Category::all()。制造:     $ categories = array();

foreach ( Category::all() as $key=> $category) {
    $categories[$category->id] = $category->name ;
}