简体   繁体   English

Laravel:创造了新的雄辩模型,但是laravel并不认识它

[英]Laravel: created new eloquent model, but laravel does not recognize it

My new model: 我的新模特:

<?php

class Style extends Eloquent {

    protected $table = 'styles';

}

Defined route: 定义的路线:

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

And the model's controller: 而模型的控制器:

<?php

class StyleController extends BaseController {

    public function postCreateStyle() {

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

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

}

And the html form: 和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>

And if I hit submit, I'm getting 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 have restarted xampp, re-imported the entire database, I have cleared the auto-load: php artisan dump-autoload but the error still exists. 我重新启动了xampp,重新导入了整个数据库,我已经清除了自动加载: php artisan dump-autoload但错误仍然存​​在。 What have I done wrong? 我做错了什么?

I don't know how Laravel's internal structure works, but the problem was caused by the migration and the model's name. 我不知道Laravel的内部结构如何工作,但问题是由迁移和模型的名称引起的。 It was the same. 它是一样的。 As suggested by @ceejayoz, I have created a new migration: create_styles_table and recreated the model: Style 正如@ceejayoz所建议的,我创建了一个新的迁移: create_styles_table并重新创建了模型: Style

I don't know how Laravel's internal structure works, but the problem was caused by the migration and the model's name. 我不知道Laravel的内部结构如何工作,但问题是由迁移和模型的名称引起的。 It was the same. 它是一样的。 As suggested by @ceejayoz, I have created a new migration: create_styles_table and recreated the model: Style 正如@ceejayoz所建议的,我创建了一个新的迁移:create_styles_table并重新创建了模型:Style

Laravel 4's lack of namespacing means you have to be a little careful with the naming of classes. Laravel 4缺少命名空间意味着你必须对类的命名有点小心。 If you do php artisan migrate:make style it'll make a new class called Style . 如果你做php artisan migrate:make style它会创建一个名为Style的新类。 If you create a Style model, Laravel may load the migration class instead of the Eloquent model you're expecting - and as such, it'll lack the expected functions. 如果你创建了一个Style模型,Laravel可能会加载迁移类而不是你期望的Eloquent模型 - 因此,它将缺少预期的功能。

In Laravel 5, namespacing means there's no conflict between the App\\Style model and the Style migration (but you do have to be a little careful still with migration names - I suggest something clearer like create_styles_table ). 在Laravel 5中,命名空间意味着App\\Style模型和Style迁移之间没有冲突(但是你必须对迁移名称有点小心 - 我建议像create_styles_table一样更清晰)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM