2014-09-09 3 views
1

laravel 및 php에 새로 추가되었으므로 컨트롤러에이 기능이 있습니다. 오류가 발생합니다. 치명적인 오류 클래스 이름은 유효한 객체 또는 문자열치명적인 오류 클래스 이름은 유효한 객체 또는 문자열이어야합니다.

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

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

이 클래스는 당신이 foreach 문에 Category::all()을 사용해야 종류라고 가정 내 모든 컨트롤러

<?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'); 

    } 


} 
+0

보다 나은 오류 메시지가 있어야합니까? 줄 번호 등? – Stromgren

+2

'$ Category :: all()'은'Category :: all()'이어야합니다 .... 카테고리는 변수가 아니라 클래스입니다 –

+0

'foreach'의 @Stromgren 행 – Fawzinov

답변

0

입니다. 만들기 : $ categories = array();

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