laravel 文档摘要

发布于:2023-03-19 ⋅ 阅读:(11) ⋅ 点赞:(0)

(laravel 文档阅读记录)


model casting


创建模型 $flag = new Flag(); 或 $flag = Flag::resolveInstance();

$request->input('date') 可以是 "2021-01-05 01:00:00" 字符串,
通过 Carbon::parse($request->input('date') ?? now_local())
它可以被转化为 CarbonInterface::DateTimeInterface

模型的属性可能由 $request->input('date') 获得

$flag = Flag::resolveInstance();
  'user_id'        => $event->assessment->user_id,
  'date_id'        => $event->assessment->date_id,
  'name'           => 'technical_focus',
  'value'          => $assessment->getAnswer('technical_focus'),
  'recorded_by_id' => $event->assessment->user_id,
$flag->utcDate()->associate($event->assessment->utc_date_id); // a date can has many flags. 这里设定了 flags table 条目的 utc_date_id 栏位

给一个模型的属性指定类型 (cast type, 比如 timestamp),方便后续模型操作 1


a Model 是一个对象
a Relation 是一个对象

$post = Post::find(1);
$comments = $post->comments;
// 这里是初始化了一个 Relation 对象, 让它完成了一次 
`SELECT * FROM Comments WHERE post_id=1;`
 的 SQL query, 找到很多条目了之后组装成一个 Collection 赋值给 $comments 

模型和 relationship 的关系是什么?一个 one-to-many relationship 里,比如 post hasMany comments ,那么:
$post 就会自带很多方法或 property 比如

use App\Models\Post;
$comments = Post::find(1)->comments;
foreach ($comments as $comment) {
    // ...

Eloquent will automatically determine the proper foreign key column for the Comment model. By convention, Eloquent will take the "snake case" name of the parent model and suffix it with _id. So, in this example, Eloquent will assume the foreign key column on the Comment model is post_id.

Once the relationship method has been defined, we can access the collection of related comments by accessing the comments property.

Since all relationships also serve as query builders, you may add further constraints to the relationship query by calling the comments method and continuing to chain conditions onto the query.

$comment = Post::find(1)->comments()
                    ->where('title', 'foo')

什么是 query builder

(initial query 1 )

relationship 提供的 methods 是 query builder 1

Since relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities.

$user->posts()->where('active', 1)->get();


Database tables are often related to one another. For example, a blog post may have many comments or an order could be related to the user who placed it.

load a relationship

The relationship won't be loaded after associating the id 是什么意思 1? 是指在 A->B 情况里, 若 B is not loaded 则 B 默认为 null

建立模型间关系: 一对多 or 多对多

建立一个 ‘模型间关系’ 的方法是:
多对多关系: sync, 它接受 model 作为参数 sync sync(what)

一对多关系: asscociate, 参数是 model 或 model id int associate(what)

relationship one-to-many relationship

a blog post has many comments
a comment belongs to a post

在 comments table 里有 post_id 栏位


relationship - 通过 belongsTo relationship

在 a model 的视角,
通过 belongsTo relationship, a model "找到" 自己的子模型(实际上找到的是 $post->comments() 这个 relationship)并通过它追加一个属于自己的子模型(构成了新一个 belongsTo relationship, 保存在隐形的联表里? 因为一对多关系不需要显式的联表)。


use App\Models\Comment;
use App\Models\Post;
$comment = new Comment(['message' => 'A new comment.']);
$post = Post::find(1);

The save method will automatically add the appropriate post_id value to the new Comment model 1. 至此 comment model 处理完毕了,存储在 comments table 的时候 post_id 栏位有值 —— 否则 post_id 栏位 缺值(所谓的未设置外键 ?),这是不对的。


在 a child model 的视角,
通过 belongsTo relationship, a child model "关联到 a new parent model" 即 a child model 为自己设定了 parentModel_id 栏位

// user belongs to an account 

use App\Models\Account;

// 这个叫做 to assign a child model to a new parent model, you may use the `associate` method. In this example, the User model defines a belongsTo relationship to the Account model. This `associate` method will set the foreign key on the child model. 
// (对此 user, 更新了 users table 里的它的 account_id 栏位)
// a new parent model 是 account
// comment belongs to a post

use App\Models\Post;



// a new parent model 是 post
// (对此 comment, 更新了 comments table 里的它的 post_id 栏位)


Schema Builder 数据结构

unsignedInteger (non-negative)


doctrine/dbal and config/database.php


欢迎关注微信公众号 关注公众号领取新人福利