dongpai9986 2015-01-26 10: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 10: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".

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部