Harea Costea
Harea Costea

Reputation: 273

Get incorrect table when tried to insert

I have a bizarre problem with my insert. So my migration is :

class CreatePhotoCategoryTable extends Migration {
public function up()
{
    Schema::create('photo_category',function($table){
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}
public function down()
{
    Schema::drop('photo_category');
}

}

My Model :

class PhotoCategory extends Eloquent {
protected $fillable = array('name');
public static $rules = array(
    'name'         =>  'required',
);
}

And my controller:

class PhotoCategory extends BaseController{
public function getAddPage(){
    return View::make('admin.photo_category.addPhotoCategory');
}
public function postCreate(){
    $validator = Validator::make(Input::all(), \PhotoCategory::$rules);
    if($validator->passes()){
        $oPhotoCategory = new \PhotoCategory();
        $oPhotoCategory->name = Input::get('name');
        $oPhotoCategory->save();
        $iLastId = $oPhotoCategory->id;
        return Redirect::to('/administration/category_photo/edit/'.$iLastId)
            ->with('message_succes','Succes');
    }
    return Redirect::to('/administration/category_photo/add')
        ->with('message_error','Error')
        ->withErrors($validator)
        ->withInput();
}

Evident my table in database is called photo_category but when I tried to save into this table I get en sql error : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'photo_categories' doesn't exist. So why my save() method get table photo_categories instead of photo_category. Please help me.

Upvotes: 1

Views: 38

Answers (2)

user4282834
user4282834

Reputation:

Make sure you name your controllers with different names from your models, as this might bring you some issues since both are classes and both have the same name. And also your Model class represents your database table so you need to specify your table name as

protected $table = 'photo_category';

Upvotes: 0

lukasgeiter
lukasgeiter

Reputation: 152860

The naming convention for database tables in Laravel is plural. So Laravel assumes from your model name PhotoCategory that your table is called photo_categories. You have two options:

  1. Change the name of your table to photo_categories
  2. Specify the table name in your model by adding:

    protected $table = 'photo_category';

Upvotes: 1

Related Questions