dongpai9986 2015-01-26 18:15
浏览 47
已采纳

Laravel在创建模型时扩展了Eloquent

When creating a basic model in Laravel, I extend the Eloquent class so that my model has all of the awesome Eloquent functionality. For example:

class Product extends \Eloquent {
    //class definition here
}

I've been reading through the Laravel framework source code, and I don't understand where this Eloquent class actually comes from. I can see there is a Illuminate\Database\Eloquent\Model class, which seems to have all of the functionality I would expect from the Eloquent class that I'm extending, but I'm confused how the inheritance is actually working since I'm extending Eloquent , not Illuminate\Database\Eloquent\Model.

How does this work?

  • 写回答

1条回答 默认 最新

  • dongzi0850 2015-01-26 18:22
    关注

    If you look at the contents of the file "config/app.php", you will find an "aliases" key towards the bottom of the file. That is what is doing all the magic under the hood. Laravel allows you to alias classes and you can add yours too to the "config/app.php" file so that you do not have to use long, fully qualified names in your code everywhere.

    As you can see, the actual class that you are extending is "Illuminate\Database\Eloquent\Model" which is aliased to "Eloquent" hence you only need to extend "Eloquent".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?