laravel 文档摘要

(laravel 文档阅读记录)

model

model casting

模型(App\Models\Flag::Illuminate\Database\Eloquent\Model)

创建模型 $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();
$flag->fill([
  '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 栏位
$flag->save();

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

relationship

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 ,那么:
1
$post 就会自带很多方法或 property 比如

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

2
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.

3
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')
                    ->first();

4
什么是 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();

relationship

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.
1

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 栏位

1

relationship - 通过 belongsTo relationship
父模型更新子模型

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

1

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

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;
 
$user->account()->associate(Account::find(10));
 
$user->save();

// 这个叫做 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;

$comment->post()->associate(Post::find(10));

$comment->save();

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

migration

Schema Builder 数据结构

https://laravel.com/docs/5.0/schema
https://laravel.com/docs/master/migrations#available-column-t...

boolean
integer
unsignedInteger (non-negative)
decimal

misc

doctrine/dbal and config/database.php
1


changsj
changsj.

changsj.

211 声望
11 粉丝
0 条评论
推荐阅读
新前端概念阅读记录1
新前端概念阅读记录1Angular 新纪元: Signals RFC备受Vue、Angular和React青睐的Signals演进史_前端 - Ryan Carniato_InfoQ 精选文章从零到一:从 初识 vue 响应式原理 到 简单设计一个 vue 3 的响应系统

changsj阅读 202

Goravel ORM 新增模型关联,用 Golang 写关联也可以跟 Laravel 简单
Goravel 是一个功能完备、具有良好扩展能力的 Web 应用程序框架。作为一个起始脚手架帮助 Golang 开发者快速构建自己的应用。框架风格与 Laravel 保持一致,让 PHPer 不用学习新的框架,也可以愉快的玩转 Golang!

韩同学的简单逻辑阅读 771

【第五篇Laravel10权限中间件】Laravel10 + Vue3.0前后端分离框架通用后台源码
②复制Laravel9 + Vue3.0前后端分离框架通用后台源码,backend/app/Http/Kernel.php 搜索 $routeMiddleware

小拼拼阅读 615

【第四篇Laravel10自定义函数】Laravel10 + Vue3.0前后端分离框架通用后台源码
① 新建文件Helpers/functions.php② 编辑composer.json在 autoload 名称中,新增 files例如: {代码...} ③composer的自动加载类命令——composer dump-autoloadvendor/composer/autoload_classmap.phpvendor/compose...

小拼拼阅读 567

【第三篇Laravel10安装依赖】Laravel10 + Vue3.0前后端分离框架通用后台源码
①用于跨域composer require fruitcake/laravel-cors②用于权限composer require laravel/passport③

小拼拼阅读 540

【第二篇Laravel10测试性能及ngnix配置】Laravel10 + Vue3.0前后端分离框架通用后台源码
章节目录【第一篇Laravel10安装】Laravel10 + Vue3.0前后端分离框架通用后台源码①vscode 打开 laravel10-vue3-admin②routes/web.php③新增如下代码 {代码...} ④运行效果,36s⑤分析性能,简单hello都要36s。连接数...

小拼拼阅读 441

changsj.

211 声望
11 粉丝
宣传栏