2015-04-28 110 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東西更清楚)。