//定义一个Eloquent模型
class User extends Model{}
// 生成一个Eloquent模型
php artisan make:model User
// 生成一个 Eloquent模型的时候,顺便生成迁移文件
php artisan make:model User --migration OR -m
// 指定一个自定义的数据表名称
class User extends Model{
protected $table = 'my_users';
}
// =================== More ====================
// 插入一条数据
Model::create(array('key'=>'value'));
// 通过属性找到第一条相匹配的数据,或创建一条新数据
Model::firstOrCreate(array('key'=>'value'));
Model::firstOrNew(array('key'=>'value'));
Model::updateOrCreate(array('search_key'=>'search_value'), array('key'=>'value'));
// 使用属性的数组来填充一个模型,用的时候要小心 Mass Assignment 安全问题
Model::fill($attributes);
Model::destroy(1);
Model::all();
Model::find(1);
Model::find(array('first','last'));// 使用双主键进行查找
Model::findOrFail(1);
Model::findOrFail(array('first','last'));
Model::where('foo','=','bar')->get();
Model::where('foo','=','bar')->count();
Model::where('foo','=','bar')->delete();
Model::where('foo','=','bar')->toSql();// 输出原始的查询语句
Model::whereRaw('foo = bar and cars = 2', array(20))->get();
Model::on('connection-name')->find(1);
Model::with('relation')->get();
Model::all()->take(10);
Model::all()->skip(10);
// 默认eloquent排序是asc
Model::all()->orderBy('column');
Model::all()->orderBy('column','desc');
// 查询json数据
Model::where('options->language','en')->get();
Model::whereJsonContains('options->languages','en')->get();
// 字段是数组
Model::whereJsonLength('options->languages',0)->get();//字段长度为0
Model::whereJsonDoesntContain('options->languages','en')->get();// 字段是数组,不包含
// 软删除
Model::withTrashed()->where('cars',2)->get();
Model::withTrashed()->where('cars',2)->restore();
Model::where('cars',2)->forceDelete();
Model::onlyTrashed()->where('cars',2)->get();// 查找只带有软删除的模型
// 模型关联
// 一对一 User::phone()
return $this->hasOne('App\Phone','foreign_key', 'local_key');
// 一对一 Phone::user(),定义相对的关联
return $this->belongsTo('App\User','foreign_key','other_key');
// 多对多 User::roles();
return $this->belongsToMany('App\Role','user_roles','user_id','role_id');
// 多对多 Role::users();
return $this->belongsToMany('App\User');
// 多对多 Retrieving intermediate table columns
$role->pivot->create_at;
// 多对多 中介表字段
return $this->belongsToMany('App\Role')->withPivot('column1','column2');
// 多对多 自动维护 create_at, update_at时间戳
return $this->belongsToMany('App\Role')->withTimestamps();
//远层一对多 Country::posts(),一个conutry模型可能难过过中介的users模型关联到多个post模型 User::country_id
return $this->hasManyThrough('App\Post','App\User','country_id','user_id');
// 多态关联 Photo::imageable()
return $this->morphTo();
// 多态关联 Staff::photos()
return $this->morphMany('App\Photo','imageable');
// 多态关联 - 在AppServiceProvider 中注册你的多态对照表
Relation::morph([
'Post' => App\Post::class,
'Comment' => App\Comment::class,
]);
// 查找关联
$user->posts()->where('active',1)->get();
$posts = App\Post::has('comments')->get();
$posts = Post::has('comments','>=',3)->get();
$posts = Post::has('comments.votes')->get();
$posts = Post::whereHas('comments',function($query){
$query->where('comment','like','foo%');
})->get();
// 预加载 -- 问?是如何实现的?
$books = App\Book::with('author')->get();
$books = App\Book::with('author','publisher')->get();
$books = App\Book::with('author.contacts')->get();
// 延迟预加载
$books->load('author','publisher');
// 写入关联模型
$comment = new App\Comment('message'=>'A new comment.']);
$post->comments()->save($comment);
// save与对对多关联
$post->comments()->saveMany([
new App\Comment(['message'=>'a new comment.']),
new App\Comment(['message'=>'another comment.']),
]);
$post->comments()->create(['message'=>'a new comment.']);
// 更新从属关联
$user->account()->associate($account);
$user->save();
$user->account()->dissociate();
$user->save();
// 附加多对多,移除多对多,省略。。。使用较少
// ==================================
// 事件
Model::retrieved(function($model){});
Model::creating(function($model){});
Model::created(function($model){});
Model::updating(function($model){}):
Model::updated(function($model){});
Model::saving(function($model){});
Model::saved(function($model){});
Model::deleting(function($model){});
Model::deleted(functon($model){});
Model::restoring(function($model){});
Model::restored(function($model){});
Model::observe(new FooObserver); // 运行观察者动作
// eloquent配置信息
// 关闭模型插入或更新操作引发的 mass assignment 异常
Eloquent::unguard();
// 重新开启 mass assignment异常抛出功能
Eloquent::reguard();
Laravel 模型使用方法
最新推荐文章于 2024-07-18 17:15:41 发布