模型类必须扩展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:
In the App folder: app/Cat.php
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.
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 | 表PK | id |
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 |
财产 | 描述 | 默认 |
---|---|---|
protected $casts | 应该转换为本机类型的属性 | [] |
在Laravel 开发过程中,用了很多诸如:laravel-admin,Guzzle,Intervention Image 等优秀的插件,看他们的 star 很多,得到很多人的关注,就想着自己能不能写个有价值的插件,共享给大家使用。 今天就让我们来说说如何创建一个简单的插件:「数字转中文大写金额」 具体只需要以下「12」个步骤: 1. 创建 Laravel 5.5 项目 // 下载最新 Larav
我需要修改我的代码,使它成为一个模型-视图-控制器。因为我是一个完全的编程新手,如果我诚实的话,我会头疼。任何帮助如何做到这一点将不胜感激。 *在一个牧场上有200只忙碌的绵羊。这群羊由95只白羊、60只黑羊和45只白黑羊组成。牧羊人现在想把它们分开,这样相应的羊毛就可以按颜色剪了。帮他写一个小脚本,这样他就可以更好地点他的羊了。请使用变量、数组、数学运算符和函数实现前三点。提示:为了更好地概述,
如何在laravel collectives中制作单选按钮? 我从数据库中获取值,我需要选择适当的性别值。 完整代码如下
正如官方文件所说: 有时,您可能不仅希望保存模型,还希望保存其所有关系。为此,您可以使用push方法:保存模型和关系$user- 术语表: term_id 姓名 鼻涕虫 Term_taxonomy表: term_taxonomy_id term_id 描述 我的学期模型: 我的术语分类模型: 我的分类控制器 用我上面的代码,我可以保存名称和段塞,但分类和描述没有插入。有可能吗? 谢谢,我是新来的。
我的控制台上有这个错误。我使用的工作从Laravel 5.4和我已经移动饲料模型在应用\模型\饲料。两天以来,我有这个错误,因为Laravel没有找到提要模型。我已经重启我的工作与PHP工匠队列:重启。 [2017-07-13 10:45:33]演出。错误:Symfony\Component\Debug\Exception\fatalthrowable错误:在/home/site\u com/ht
我正在从事一个laravel项目,其中我设置了一个自定义主键。但是,项目允许两个不同的条目在设置为主键的字段中具有相同的值。我如何限制这一点? 我已将模型上的public$incrementing属性设置为false,并将protected$keyType属性设置为string。 我预计代码将引发一个错误,如果我输入两个记录将相同的值在'reg_no'字段,这被定义为主要的,但记录被保存没有错误。