Laravel对Symfony |模型属性 - php

我实际上是在发现Laravel(我已经使用symfony多年了),并且遇到一种情况,我不知道是否只是找不到正确的信息,或者这只是在Laravel中要做的事情。

在Symfony中创建模型时,请将属性放在模型类中。实际上,这些属性代表ORM表的不同列。

现在在Laravel中,我看到所有人都没有将这些属性放在模型类中,而是在迁移文件中。因此,应该为新项目做贡献的新开发人员将不得不研究数据库或迁移文件。我认为这不是ORM的规则:“让开发人员思考类而不是表”

有人可以请问一下这一点吗?

谢谢

参考方案

如果您使用PHPStorm(但它也可以与其他IDE一起使用),那么有一个有趣的composer软件包可以为您生成模型文档,以便根据您的迁移自动完成属性广告方法的填充:

https://github.com/barryvdh/laravel-ide-helper

其三种主要方法:

php artisan ide-helper:generate为立面创建phpDoc
php artisan ide-helper:model为您的模型创建phpDoc(每次新迁移后应重新启动)
php artisan ide-helper:meta创建phpstorm元文件

因此,如果启动php artisan ide-helper:model,它将添加与此类似的内容

/**
 * App\User
 *
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User permission($permissions)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User role($roles, $guard = null)
 * @property int $id
 * @property string $name
 * @property string $email
 * @property \Illuminate\Support\Carbon|null $email_verified_at
 * @property string $password
 * @property string|null $remember_token
 * @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 whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmailVerifiedAt($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\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
 * [...]
 */

所以你有一个phpDoc

并且您的IDE应该能够像这样自动完成代码:

Laravel对Symfony |模型属性 - php

Laravel Blade模板尝试获取非对象的属性时如何返回null而不是ErrorException - php

我正在编写一些Laravel Blade模板,并且我的模型可能包含空对象。我非常想尝试获取对象属性,如果有错误,则返回null。因此,不必编写以下代码:@if ($model->child_object_that_may_be_null) {{ $model->child_object_that_may_be_null->interesti…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP-如何获取类的成员函数列表? - php

如果我知道班级的名字。有没有办法知道类的成员函数列表? 参考方案 get_class_methods()是你的朋友

Laravel-无需登录即可认证用户 - php

在laravel 5.4中,可以在不登录用户的情况下对用户进行身份验证吗?从laravel doc中,我能找到的最接近的东西是:Auth::once($credentials) 但这仍然使该用户感到厌烦。我需要做的就是只是知道使用该电子邮件和密码的用户是否存在。 参考方案 您可以将Auth::attempt函数与第三个参数用作false进行登录 $email…

php ziparchive类源代码 - php

Improve this question 我如何获取ziparchive类本身的源代码。 参考方案 假设您在谈论PHP ZipArchive class:下载PHP source code并查找适当的文件。如果您希望源代码是PHP代码,您可能会感到失望,因为源代码是用C语言编写的。或者,也可以在PHP Github Development Reposito…