简体   繁体   中英

Laravel eloquent - Doctrine features equivalent

Coming from a long Symfony-Doctrine background, I have started learning Laravel 8.

One of my first discovery was that migration needed to be manually created after using make:migration (from what I understood thus far) in both Models and Migration.

Symfony, with Doctrine, allowed a bunch of automatisation, and I only needed to create the field or relation from the Model (php annotation or yaml) - before launching doctrine:schema:validate and make:migration
https://symfony.com/doc/current/doctrine.html#migrations-adding-more-fields .
Let 'say I create a Post and Comment entity, with a One-To-Many relationship.
If I define the relation in the php classes

class Comment extends Model
{

    /**
     * Get the Post owning this comment
     */
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

Post class

class Post extends Model
{    
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

Is possible to generate the migration scripts that would update the database and create these relation ship? Or will I have to rewrite it twice (once in the PHP class with hasMany/BelongsTo then again in the Migtration file?
Not having a central file to map/read a model (which linkied php and database) seems weird to me now.
I've only started but the documentation does not seems to mention anything equivalent

: for more clarity: I'm asking if there are equivalent of Generating migration code/script from models (or a central mapping file: yaml or annotation ) without having to write fields to both migration and models ( in $fillable or other fields...):为了更清楚:我问是否有等效的从模型生成迁移代码/脚本(或中央映射文件:yaml 或注释),而不必将字段写入迁移和模型(在 $fillable 或其他字段中...)

Closest thing I could find is this https://github.com/laracasts/Laravel-5-Generators-Extended Another use case where this is cumbersome: many-to-many migrations Having to manually write that third middle table from scratch is really something I wish was made automatically.我能找到的最接近的是https://github.com/laracasts/Laravel-5-Generators-Extended另一个麻烦的用例:多对多迁移 不得不从头开始手动编写第三个中间表真的是我希望自动制作的东西。

In laravel framework despite other famous fameworks like symfony or python django, you're responsible for making database migration files. By this way, you are free to customize your database schema and add any database constrains like (unique constrain, relation constrains and etc.). Also you can add any raw sql using \DB::unprepared(); in your migration files.

I can tell you that it is normal to be a little confuse about this flow, because you've used to this kind of automation in other frameworks like I did, but beilieve me, you get to used to this flow.

By the way, there are some packages out there that do this automation (create migration files according to model) for you.

For creating a migration you need to run command

php artisan make:migration migration_name

And for creating a model you need to run command

php artisan make:model table_name

Refer to this link https://laravel.com/docs/8.x/eloquent#generating-model-classes

By creating a model you create a table, where you can define the columns types and different properties and define the relationship with other tables. By creating a migration file you can define the table columns and the constraints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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