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

问题描述 投票:0回答:2

我是 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');

    }


}
php laravel laravel-4
2个回答
5
投票

假设该类称为 Category,您应该在 foreach 中使用

Category::all()
。制作: $类别=数组();

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

0
投票

就我而言,我有一个错误的语法,例如:

$self::SOME_CONSTANT

刚刚将其更正为以下内容以再次滚动:

self::SOME_CONSTANT
© www.soinside.com 2019 - 2024. All rights reserved.