欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel extends Eloquent extends model 区别 有大用 有大大用

询问 
观看了1 万次
19

Laravel 4中所有Eloquent模型的示例都扩展了Eloquent,但是当您在Laravel 5中生成模型时,它说扩展了Model,它们是一样的吗?

Laravel 4

<?php

class User extends Eloquent {

    //Code

}

Laravel 5

<?php

class User extends Model {

    //Code

}

Laravel 5文档说:

定义雄辩的模型

class User extends Model {}

3个答案  正确答案

20

是的,他们是一样的。Laravel 4使用类别名来映射Illuminate\Database\Eloquent\ModelEloquent您可以在app/config/app.php文件中看到

'Eloquent'          => 'Illuminate\Database\Eloquent\Model',

Laravel 5改用了命名空间。因此,在模型类的顶部,您将看到以下行:

use Illuminate\Database\Eloquent\Model;
1个

用过的...

use Illuminate\Database\Eloquent\Model;

扩展模型

0

我正在使用来自barryvdh的laravel-ide-helper的_ide_helper.php文件

那里的子类也称为Eloquent,并扩展到Model。

因此,如果我将自己的模型类扩展到Eloquent,则IDE会知道所有函数,例如MyModelClass :: find。也许还有另一种方法可以做到,但这确实对我有用。

你的答案

来自 https://stackoverflow.com/questions/31837869/extends-model-extends-eloquent



Asked 
Viewed 10k times
19

All the examples of Eloquent Models in Laravel 4 extends Eloquent, but when you generate a Model in Laravel 5 it says extends Model, are they the same?

Laravel 4

<?php

class User extends Eloquent {

    //Code

}

Laravel 5

<?php

class User extends Model {

    //Code

}

The Laravel 5 docs says:

Defining An Eloquent Model

class User extends Model {}

3 Answers  正确答案


20

Yes they are the same. Laravel 4 uses Class Aliasing to map Illuminate\Database\Eloquent\Model to Eloquent. You can see in the app/config/app.php file:

'Eloquent'          => 'Illuminate\Database\Eloquent\Model',

Laravel 5 uses namespacing instead. So at the top of the model class you will see this line:

use Illuminate\Database\Eloquent\Model;
  • I'm just getting started with Laravel, v5.6. My model has BOTH use Illuminate\Database\Eloquent\Model as Eloquent; AND class XYZ extends Eloquent -- is having BOTH okay? > <?php > use Illuminate\Database\Eloquent\Model as Eloquent; > class User extends Eloquent {` – Genki Jan 22 '19 at 5:17 
  • @Genki, yes. The use ... as ... statement is a way of aliasing a class name but only in that file. Hope that makes sense. – Patrick Stephan Jan 22 '19 at 18:29
  • Thank you for the explanation. – Genki Feb 3 '19 at 23:38
1

used...

use Illuminate\Database\Eloquent\Model;

to extend the model

0

I am using the _ide_helper.php file from barryvdh's laravel-ide-helper

The subclass there is also called Eloquent and extends to Model.

So if I extend my own model class to Eloquent, the IDE knows all the functions, like MyModelClass::find. Maybe there's another way to do it, but that really works for me.


Your Answer

来自  https://stackoverflow.com/questions/31837869/extends-model-extends-eloquent






普通分类: