Laravel: created a new eloquent model, but laravel doesn't recognize it
My new model:
<?php
class Style extends Eloquent {
protected $table = 'styles';
}
Specific route:
Route::group(array('prefix' => '/templates'), function(){
Route::post('/style-create', array('uses' => 'StyleController@postCreateStyle', 'as' => 'postCreateStyle'));
});
And the model controller:
<?php
class StyleController extends BaseController {
public function postCreateStyle() {
$style = new Style();
$style->save();
return Redirect::route('getStyleHome');
}
}
And the html form:
<form role="form" method="post" action="{{ URL::route('postCreateStyle') }}">
<!-- FIELDS -->
<input type="submit" value="{{ isset($style) ? 'Save' : 'Create' }} template" class="btn btn-lg btn-primary" />
</form>
And if I hit submit, I get this error:
[2015-04-28 14:11:59] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to undefined method Style::save()' in C:\xampp\htdocs\cspage\app\controllers\StyleController.php:18
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []
I restarted xampp, re-imported the whole database, I cleared the autoload: php artisan dump-autoload
but the error still exists. What I did wrong?
source to share
I don't know how Laravel's internal framework works, but the problem was caused by migration and model name. Same. As @ceejayoz suggested, I created a new migration: create_styles_table and recreated the model: Style
Laravel 4 The absence of a namespace means that you have to be careful with naming classes. If you do php artisan migrate:make style
it will create a new class called Style
. If you create a model Style
, Laravel may load the migration class instead of the expected Eloquent model, and as such it will not have the expected functionality.
In Laravel 5, namespace means no conflict between model App\Style
and migration Style
(but you need to be a little careful with migration names - I suggest something clearer like create_styles_table
).
source to share