这篇文章主要介绍了关于laravel之eloquent orm ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
![1531201901928009.png 814260385-5b42b5412241d_articlex[1].png](https://img.php.cn//upload/image/259/207/792/1531201901928009.png)
![1531201908494806.png 2216285623-5b42b5508a1b4_articlex[1].png](https://img.php.cn//upload/image/377/401/809/1531201908494806.png)
Active Record 是一种数据访问设计模式,它可以帮助你实现数据对象Object到关系数据库的映射。应用Active Record时,每一个类的实例对象唯一对应一个数据库表的一行(一对一关系)。你只需继承一个abstract Active Record 类就可以使用该设计模式访问数据库,其最大的好处是使用非常简单
![1531201928990874.png 974427235-5b42b55b5a863_articlex[1].png](https://img.php.cn//upload/image/961/409/397/1531201928990874.png)
![1531201960341708.png 148572410-5b42b5679400e_articlex[1].png](https://img.php.cn//upload/image/540/111/156/1531201960341708.png)
https://github.com/barryvdh/l...
Installation:
composer require barryvdh/laravel-debugbar --dev
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->string('phone');
$table->unsignedInteger('user_id');
//显示的声明外键:通知数据库根据外键关联表和建立索引,提高运行速度
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
}public function profile()
{
return $this->hasOne(Profile::class);
}public function user()
{
return $this->belongsTo(User::class);
}![1531201978626122.png 2016152457-5b42c2770c954_articlex[1].png](https://img.php.cn//upload/image/711/596/766/1531201978626122.png)
自定义外键:
return $this->hasOne(Profile::class,'显示指定自定义外键');
依赖注入Request $request,获取当前登录用户$request->user()
Route::get('/test',function (Request $request){
//反向
// $profile = \App\Profile::find(1);
// dd($profile->user);
$user = $request->user();
// if (is_null($user->profile)){
// $user->profile()->create([
// 'phone' => '15801340269'
// ]);
// }
//用firstOrCreate改进if
$user->profile()->firstOrCreate(['user_id' => $user->id],[
'phone' => '18363046291'
]);
//访问属性一样访问方法
dd($user->profile);
});![1531201989311175.png 1821403919-5b42c83248e80_articlex[1].png](https://img.php.cn//upload/image/852/674/178/1531201989311175.png)
1:N hasMany(XXX:class) 反之:belongsTo(XXX:class)
![1531202004514341.png 3752176986-5b42c9e330eef_articlex[1].png](https://img.php.cn//upload/image/478/430/493/1531202004514341.png)
![1531202040878489.png 3763343682-5b3edff3ec3fb_articlex[1].png](https://img.php.cn//upload/image/983/739/297/1531202040878489.png)
![1531202047663554.png 1551414493-5b42d0908f89b_articlex[1].png](https://img.php.cn//upload/image/435/793/369/1531202047663554.png)
中间表命名:按照A-Z首字母排序
public function users()
{
return $this->belongsToMany(User::class);
}
public function habits()
{
return $this->belongsToMany(Habit::class);
}![1531202058946574.png 2030140819-5b42f68d2ebb5_articlex[1].png](https://img.php.cn//upload/image/618/821/710/1531202058946574.png)
detach解绑,sync方法用的比较多,只保留1,2![1531202066341134.png 1767484339-5b42f77d7faad_articlex[1].png](https://img.php.cn//upload/image/462/758/493/1531202066341134.png)
![1531202076215309.png 1407700575-5b42fa29d7e67_articlex[1].png](https://img.php.cn//upload/image/331/331/763/1531202076215309.png)
数据表:
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - stringclass Country extends Model
{
protected $fillable = ['name'];
/**
* 获得某个国家下所有的用户文章。
*/
public function papers()
{
return $this->hasManyThrough(Paper::class,User::class);
}
}$factory->define(App\Paper::class, function (Faker $faker) {
return [
'title' => $faker->sentence,
'user_id' => \App\User::all()->random()->id,
];
});![1531202088297717.png 1645904773-5b43054aa5c24_articlex[1].png](https://img.php.cn//upload/image/151/422/558/1531202088297717.png)
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'country_id' => \App\Country::all()->random()->id,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgpFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});![1531202094256737.png 2472489467-5b430554901f2_articlex[1].png](https://img.php.cn//upload/image/852/287/728/1531202094256737.png)
获取每个国家论文总数:
![1531202102778054.png 468658695-5b430428cb53b_articlex[1].png](https://img.php.cn//upload/image/983/336/540/1531202102778054.png)
面向对象多态:运行时加载机制
![1531202110449025.png 3328482421-5b430609370ff_articlex[1].png](https://img.php.cn//upload/image/143/554/892/1531202110449025.png)
更多:https://laravel-china.org/doc...
伪造数据:![1531202120156807.png 3802634324-5b4309f000d82_articlex[1].png](https://img.php.cn//upload/image/395/877/214/1531202120156807.png)
![1531202128696877.png 1929693901-5b4317904c02f_articlex[1].png](https://img.php.cn//upload/image/800/113/897/1531202128696877.png)
除了传统的多态关联,您也可以定义「多对多」的多态关联。例如,Post 模型和 Video 模型可以共享一个多态关联至 Tag 模型。 使用多对多多态关联可以让您在文章和视频中共享唯一的标签列表。
更多:https://laravel-china.org/doc...
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上就是关于Laravel的Eloquent ORM的解析的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号