Laravel 制作模型

例子

模型制作

模型类必须扩展Illuminate\Database\Eloquent\Model。模型的默认位置是/app目录。

可以通过Artisan命令轻松生成模型类:

php artisan make:model [ModelName]

这将app/默认创建一个名为的新PHP文件[ModelName].php,并将包含新模型的所有样板,包括基本设置所需的类,名称空间和using。

如果要与模型一起创建迁移文件,请使用以下命令,其中-m还将生成迁移文件:

php artisan make:model [ModelName] -m

In addition to creating the model, this creates a database migration that is hooked up to the model. The database migration PHP file is located by default in database/migrations/. This does not--by default--include anything other than the id and created_at/updated_at columns, so you will need to edit the file to provide additional columns.

Note that you will have to run the migration (once you have set up the migration file) in order for the model to start working by using php artisan migrate from project root

In addition, if you wish to add a migration later, after making the model, you can do so by running:

php artisan make:migration [migration name]


Say for example you wanted to create a model for your Cats, you would have two choices, to create with or without a migration. You would chose to create without migration if you already had a cats table or did not want to create one at this time.

For this example we want to create a migration because we don't already have a table so would run the following command.

php artisan make:model Cat -m

This command will create two files:

  1. In the App folder: app/Cat.php

  2. In the database folder: database/migrations/timestamp_creat_cats_table.php

The file we are interested in is the latter as it is this file that we can decide what we want the table to look like and include. For any predefined migration we are given an auto incrementing id column and a timestamps columns.

The below example of an extract of the migration file includes the above predefined columns as well as the addition of a the name of the cat, age and colour:

public function up()
    {
        Schema::create('cats', function (Blueprint $table) {

            $table->increments('id');    //预定义ID
            $table->string('name');      //Name
            $table->integer('age');      //Age
            $table->string('colour');    //Colour
            $table->timestamps();        //预定义时间戳

        });
    }

So as you can see it is relatively easy to create the model and migration for a table. Then to execute the migration and create it in your data base you would run the following command:

php artisan migrate

Which will migrate any outstanding migrations to your database.


Model File Location

Models can be stored anywhere thanks to PSR4.

By default models are created in the app directory with the namespace of App. For more complex applications it's usually recommended to store models within their own folders in a structure that makes sense to your apps architecture.

例如,如果您有一个使用一系列水果作为模型的应用程序,则可以创建一个名为的文件夹,app/Fruits并在创建的该文件夹内Banana.php(保持StudlyCase命名约定),然后可以在App\Fruits名称空间中创建Banana类:

namespace App\Fruits;

use Illuminate\Database\Eloquent\Model;

class Banana extends Model {
    // Implementation of "Banana" omitted
}

型号配置

雄辩的遵循“约定之上的配置”方法。通过扩展基Model类,所有模型都继承下面列出的属性。除非覆盖,否则将应用以下默认值:

财产描述默认
protected $connection数据库连接名称默认数据库连接
protected $table表名默认情况下,类名将转换为snake_case复数形式。例如,SpecialPerson变为special_people
protected $primaryKey表PKid
public $incrementing指示ID是否自动递增true
public $timestamps指示是否应为模型加盖时间戳true
const CREATED_AT创建时间戳列的名称created_at
const UPDATED_AT修改时间戳列的名称updated_at
protected $dates除时间戳属性外,还应突变为DateTime的属性[]
protected $dateFormat日期属性将被保留的格式当前SQL方言的默认值。
protected $with与模型的热切关系[]
protected $hidden模型序列化中省略的属性[]
protected $visible模型序列化中允许的属性[]
protected $appends属性访问器已添加到模型序列化[]
protected $fillable可大规模分配的属性[]
protected $guarded批量分配中列入黑名单的属性[*] (所有属性)
protected $touches保存时应接触的关系[]
protected $perPage返回分页的模型数量。15
5.0
财产描述默认
protected $casts应该转换为本机类型的属性[]