标签:多态 nta protected ide art rap 视频 find div
laravel 关联模型 多态关系
1个关联上从属于多个模型,如:博客post
和用户user
共享1个关联图片image
。
1篇博客拥有1张主图
1个用户拥有1个头像
post ( id, title, content)
user ( id, name )
image ( id, imageable_id, imageable_type, url)
#`imageable_id`关联表ID,`imageable_type`关联模型类名
class Image extends Model
{
const TABLE = ‘image‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘imageable_id‘,‘imageable_type‘,‘url‘,‘created_at‘,‘updated_at‘ ];
public function imageable()
{
return $this->morphTo();
}
}
class Post extends Model
{
const TABLE = ‘post‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘title‘,‘content‘,‘created_at‘,‘updated_at‘ ];
public function image()
{
return $this->morphOne(Image::class, ‘imageable‘);
}
}
class User extends Model
{
const TABLE = ‘user‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘name‘,‘created_at‘,‘updated_at‘ ];
public function image()
{
return $this->morphOne(Image::class, ‘imageable‘);
}
}
查询关联
image表
ID imageable_id imageable_type url
1 1 App\Model\Post xxx
2 1 App\Model\User xxxx
Post::with(‘image‘)->find(1)->toArray();
User::with(‘image‘)->find(1)->toArray();
#反向查询
Image::with(‘imageable‘)->find(1)->toArray();
添加关联
#非关联添加,直接新增关联表
Image::create([‘imageable_id‘ => 2,‘imageable_type‘ => Post::class,‘url‘ => ‘xxx‘]);
#关联添加,不用给关联字段赋值,推荐使用
$post = Post::find(1);
$post->image()->create([‘url‘=>‘x‘]);
更新关联
#非关联更新,直接更新关联表对应记录
Image::where([‘imageable_id‘=>2,‘imageable_type‘=>Post::class)->update([‘url‘=>‘new url‘]);
#关联更新,推荐使用
$post = Post::find(1);
$post->image()->update([‘url‘=>‘new url‘]);
#update针对‘一对一’,‘一对多’应该删除对应的所有关联再新增
删除关联
#非关联删除,直接删除关联表对应记录
Image::where([‘imageable_id‘=>2,‘imageable_type‘=>Post::class)->delete();
#关联删除,推荐使用
$post = Post::find(1);
$post->image()->delete();
1个关联上从属于多个模型,如:文章article
和视频video
共享1个关联评论comment
。
1篇文章拥有多个评论
1个视频拥有多个评论
article ( id, name )
video ( id, title, url )
comment ( id, commentable_id, commentable_type, content)
#`commentable_id`关联表ID,`commentable_type`关联模型类名
class Comment extends Model
{
const TABLE = ‘comment‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘commentable_id‘,‘commentable_type‘,‘content‘,‘created_at‘,‘updated_at‘ ];
public function commentable()
{
return $this->morphTo();
}
}
class Article extends Model
{
const TABLE = ‘article‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘name‘,‘created_at‘,‘updated_at‘ ];
public function comments()
{
return $this->morphMany(Comment::class,‘commentable‘);
}
}
class Video extends Model
{
const TABLE = ‘video‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘title‘,‘url‘,‘created_at‘,‘updated_at‘ ];
public function comments()
{
return $this->morphMany(Comment::class,‘commentable‘);
}
}
查询关联
Article::with(‘comments‘)->find(1)->toArray();
Video::with(‘comments‘)->find(1)->toArray();
#反向查询
Comment::with(‘commentable‘)->find(1)->toArray();
高级1点的查询
Article::has(‘comments‘)->get()->toArray(); #查询至少1条评论的文章
Article::has(‘comments‘,‘>=‘,5)->get()->toArray(); #查询至少5条评论的文章
#查询评论包含‘xx’的文章
Article::whereHas(‘comments‘, function (Builder $query) {
$query->where(‘content‘, ‘like‘, ‘%xx%‘);
})->get()->toArray();
#查询评论 包含‘xx’ & 至少5条评论 的文章
Article::whereHas(‘comments‘, function (Builder $query) {
$query->where(‘content‘, ‘like‘, ‘%xx%‘);
}, ‘>=‘, 5)->get()->toArray();
添加关联
$article = Article::find(3);
$article->comments()->save(new Comment([‘content‘=>‘new new‘]))
$article->comments()->create([‘content‘ => ‘good good study‘]);
$article->comments()->createMany([
[‘content‘ => ‘good good study‘],
[‘content‘ => ‘day day up‘]
]);
删除关联
$article = Article::find(3);
$article->comments()->where(‘id‘,7)->delete(); #删除关联表comment id=7记录
$article->comments()->delete(); #删除所有关联
更新关联
#更新指定评论
$article = Article::find(3);
$article->comments()->where(‘id‘,6)->update([‘content‘=>‘new contetn‘]);
#先删除所有关联,再添加关联。
使用多对多多态关联允许使用一个唯一标签在博客文章和视频间共享。
1篇文章对应多个标签,1个标签对应多篇文章
1个视频对应多个标签,1个标签对应多个视频
1个标签既可以是文章的标签,也是视频的标签,即文章和视频共享同1个标签
article ( id, name )
video ( id, title, url )
tag ( id, name )
tagable ( tag_id, tagable_id, tagable_type )
#`tagable_id`关联表ID,`tagable_type`关联模型类名
class Tag extends Model
{
const TABLE = ‘tag‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘name‘,‘created_at‘,‘updated_at‘ ];
public function articles()
{
return $this->morphedByMany(Article::class, ‘tagable‘, Tagable::TABLE);
}
public function videos()
{
return $this->morphedByMany(Video::class, ‘tagable‘, Tagable::TABLE);
}
}
class Tagable extends Model
{
const TABLE = ‘tagable‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘tag_id‘,‘tagable_id‘,‘tagable_type‘,‘created_at‘,‘updated_at‘ ];
}
class Article extends Model
{
const TABLE = ‘article‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘name‘,‘created_at‘,‘updated_at‘ ];
public function tags()
{
return $this->morphToMany(Tag::class, ‘tagable‘, Tagable::TABLE);
}
}
class Video extends Model
{
const TABLE = ‘video‘;
protected $table = self::TABLE;
protected $fillable = [ ‘id‘,‘title‘,‘url‘,‘created_at‘,‘updated_at‘ ];
public function tags()
{
return $this->morphToMany(Tag::class, ‘tagable‘, Tagable::TABLE);
}
}
注意
morphToMany($related, $name, $table = null)
morphedByMany($related, $name, $table = null)
//$table=null,默认为$name + s
查询关联
Article::with(‘tags‘)->find(1)->toArray();
Video::with(‘tags‘)->find(1)->toArray();
//反向查询
Tag::with(‘articles‘)->find(1)->toArray();
添加关联
$article->tags()->create([‘name‘ => ‘神话‘]);
#新增记录,标签表tag & 关联表tagable,同时新增
$article->tags()->attach(4);
#新增记录,仅新增关联表tagable
$article->tags()->attach([
7 => [‘created_at‘ => ‘2020-07-08 11:01:27‘],
8 => [‘created_at‘ => ‘2020-07-08 11:01:27‘],
]);
同步关联
$article = Article::find(1);
$article->tags()->sync([1,3]);
//sync(),等同于,新增或保留1,3,删除其他
删除关联
$article = Article::find(1);
$article->tags()->detach([1,3]);
//detach($tagIds); $tagIds: 数组,要删除的id
// 为null即不传参数时,删除对应的所有关联
标签:多态 nta protected ide art rap 视频 find div
原文地址:https://www.cnblogs.com/mg007/p/13268704.html