3

I am working on laravel and following the tutorial http://technetlk.blogspot.com.au/2012/09/laravel-backbonejs-coffeescript_3527.html. currently working on 7th part of this tutorial.

in routes.php I have written

Route::any('api/category/(:num?)',
   array('as' => 'api.category',
        'uses' => 'api.category@index')
);

In api/category.php,

<?php
class Api_Category_Controller extends Base_Controller
{
    public $restful = true;
    public function get_index($id = null) 
    {
        if (is_null($id ))
        {
            $allCats = Category::all();
            return BaseModel::allToJson($allCats);
        }
        else
        {
            $cat = Category::find($id);
            return $cat->toJson();            
        }
    }
    public function post_index()
    {
            $cat = Input::json();
            $dbCat = new Category();
            $dbCat->code = $cat->code;
            $dbCat->name = $cat->name;
            $dbCat->save();
            return $dbCat->toJson();
    }

    public function put_index()
    {
            $cat = Input::json();
            $dbCat = Category::find($cat->id);
            $dbCat->code = $cat->code;
            $dbCat->name = $cat->name;
            $dbCat->save();
            return $dbCat->toJson();
    }
    public function delete_index($id = null)
    {
            $dbCat = Category::find($id);
            $dbCat->delete();        
    }    
}

?>

and in BaseModel.php

<?php
class Category extends BaseModel 
{
    public static $table = 'tbl_category';
}
class BaseModel extends Eloquent 
{
   public function toJson()
   {
     return json_encode($this->to_array());
   }
   public static function allToJson($array)
   {
      $temp = array();
      foreach($array as $t)
      {
          $temp[] = $t->to_array();                
      }
      return json_encode($temp);                    
    }
}
?> 

when I am trying to run

curl -X POST http://lbc.dev/api/category -H "Content-Type: application/json" –d '{"code":"cat1", "name":"Category One"}'

I am getting the follwing error

Unhandled Exception

Message:

Class 'Category' not found Location:

C:\xampp\htdocs\NewBlog\application\controllers\api\category.php on line 9

2 Answers 2

1

Your Category Model should be located in /application/models/category.php

Your BaseModel class should be located in /application/models/basemodel.php

The following is if your BaseModel class is not in a defined Autoloader directory:

Another problem you may face is the BaseModel not autoloading correctly. This can be fixed by appending this line of code to your start.php file found in your application folder:

// Autoloader::map
'BaseModel' => path('app').'/path/to/basemodel.php',
Sign up to request clarification or add additional context in comments.

3 Comments

thanx Joel, but I am not getting what this path of basemodel is path('app').'/path/to/basemodel.php'
You probably won't need to map to the BaseModel, but make sure that both are within application/models.
Jason Lewis is right. The Autoloader::map code is only needed if you don't save your BaseModel class to "application/models/basemodel.php".
1

U have to specify the model in controller file before using it

Add this line above the following line in your controller file

use App\Category;

class Api_Category_Controller extends Base_Controller {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.