(I come from Visual Studio + Entity Framework background and trying to locate equivalent functionality in Laravel + Eloquent)

In EF and Visual Studio, we add a new Model to our application and just tell it about our existing database. EF can then generate Models for my tables with public properties for columns. This gives us all those IDE and compiler benefits such as Intellisense, spelling-error detection etc.

I've recently stated exploring VS Code, Laravel and Eloquent. Going through all those tutorials and articles, I'm not sure when and how these properties are generated in the model classes. I just tried artisan make:model command and it did generate the model class, but there are no properties in it. So,

  1. 我应该手写吗?(真的吗?)
  2. 这些只是带有getter/setter的公共变量还是标准属性(请原谅我的.NET心态:)?
  3. Is there a tool/extension that could examine my database and create models with properties for their columns?

使现代化

To the people who answered my question, thanks a lot. Plus some of the comments I posted were due to my ignorance about PHP's (strange IMO) approach about member access; I just found out that PHP does not complain about non-existing class members and instead generates them on the fly (e.g. $post->NonExistingMember = SomeValue runs okay; this would not even compile in most other languages that I know). Big surprise for me. I have used C++, VB, C#, Java among several other languages and haven't seen that behavior anywhere else. All those languages would throw a compile-time error straight away saying something like Type X does not contain a member named Y. Cannot see how PHP's different approach fits together with OOP.

The actual problem that I posted this question for still remains unresolved. Although I can use reliese/laravel to generate Model classes for my database, the tool still does not generate class members against table columns, so I do not get auto-complete benefits. I'd love to hear from the experts if that can be done (automatically of course).

使现代化 2

现在我对Laravel环境有了更好的了解,我想我应该分享一下我的经验.见下面我的答案.

推荐答案

现在,我已经花了一些时间来学习Laravel、Elount和PHP,我将与大家分享一些东西,希望它们能帮助其他初学者.

PHP是一种动态语言,其代码是动态编译的(这与C#和VB.NET不同).您的模型类不需要显式定义成员以使其可访问/可分配,因此只要它们扩展Model(Laravel Eloquent中的内置类),就可以将值分配给与基础数据库表列同名的不存在成员,Eloquent将为您将其存储在数据库中.例如,如果数据库中有一个posts表,其列名为body,则可以编写以下代码在数据库中创建新记录:

$p = new Post;
$p->body = 'Some stuff';
$p->save();

Of course you need to have a class Post in your project that extends from Model but you don't need to define a member body inside that class. This would sound strange to the people coming from .NET world, but that's how dynamic languages work.

至于自动生成模型,Laravel包括可以为您生成模型的内置命令(php artisan make:model).

最后,对于intellisense和auto complete,使用与Laravel本身相同的工具,即DocBlocks.这些是PHP中的特殊注释类型,您可以使用它们记录代码元素.因此,您可以向所有包含属性名称和类型的模型类添加DocBlocks.幸运的是,VS代码中有一个非常简洁的扩展,可以自动为您实现这一点.使用以下命令安装它:

composer require --dev barryvdh/laravel-ide-helper

现在运行以下命令为所有模型类生成docblock(显然在此之前您应该已经生成了数据库和模型):

php artisan ide-helper:models --dir='app'

扩展将获取数据库的 struct ,并将DocBlocks注入所有模型,如下所示:

/**
 * App\User
 *
 * @property int $id
 * @property string $name
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Exam whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class User extends Model
{
}

VS Code will now show you table field names in model properties, like this (see how intellisense brings up name member from our DocBlocks as we type na...):

enter image description here Note that I also have Intelephense installed in my VS Code, though I'm not sure if that is required for auto-complete feature to work.

Laravel相关问答推荐

Laravel:如何从不断更新的Controller中分页数据

当使用Craftable PRO时,Wysiwig是什么时候生成的?

laravel如何在Blade 模板中将元素添加到数组

Inertiajs - 使用链接组件添加记录但无法在不使用表单的情况下清除文本区域

如何通过单击进入 vs 代码中的类

Laravel 6 Passport 在错误的凭证上返回 400 Bad request

Laravel 5.4 中的自定义助手类

链接到容器时如何使用主机网络?

控制器中的 Laravel Auth 用户 ID

Laravel - 超过 PHP 最大上传大小限制时验证文件大小

在表单中添加一对多 - Backpack laravel

从 sqlite DB 登录 Laravel,得到PDOException 找不到驱动程序

laravel 5 - assets资源 list 中未定义 css 文件?

调用未定义的方法 Maatwebsite\Excel\Excel::load()

Laravel Passport 通过访问令牌获取客户端 ID

Laravel 邮件密件抄送

Laravel 应用程序连接数据库时速度很慢

Laravel 事件、监听器、作业(job)、队列之间的区别

Laravel/PHPUnit:断言 json 元素存在而不定义值

Laravel - artisan不工作