2015-04-28 117 views
3

我的新模式:Laravel:创建新的雄辩模式,但laravel不承认它

<?php 

class Style extends Eloquent { 

    protected $table = 'styles'; 

} 

定义路线:

Route::group(array('prefix' => '/templates'), function(){ 
    Route::post('/style-create', array('uses' => '[email protected]', 'as' => 'postCreateStyle')); 
}); 

和模型的控制器:

<?php 

class StyleController extends BaseController { 

    public function postCreateStyle() { 

     $style = new Style(); 
     $style->save(); 

     return Redirect::route('getStyleHome'); 
    } 

} 

而且html格式:

<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> 

如果我点击提交,我得到这个错误:

[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} [] [] 

我已经重新启动XAMPP,重新导入整个数据库,我已经清除了自动加载:php artisan dump-autoload但错误依然存在。我做错了什么?

+1

你有任何机会称为'style'吗?另外,我认为这是L4? – ceejayoz

+0

版本:L4或L5? –

+0

尝试'php composer dump-autoload'。 –

回答

0

我不知道Laravel的内部结构是如何工作的,但问题是由迁移和模型名称引起的。这是一样的。正如@ceejayoz的建议,我已经创建了一个新的迁移:create_styles_table并重新创建模型:Style

0

I don't know how Laravel's internal structure works, but the problem was caused by the migration and the model's name. It was the same. As suggested by @ceejayoz, I have created a new migration: create_styles_table and recreated the model: Style

Laravel 4缺乏命名空间意味着你必须与类的命名小心一点。如果你做php artisan migrate:make style它会创建一个新的类Style。如果您创建了Style模型,则Laravel可能会加载迁移类,而不是您期望的Eloquent模型 - 因此,它将缺少预期的功能。

在Laravel 5,命名空间意味着有该App\Style模型和Style移民之间没有冲突(但你必须要小心一点仍与迁移的名字 - 我建议像create_styles_table东西更清楚)。