首页 > 其他分享 >22. Laravel 模型关联:更多使用方法 – 2

22. Laravel 模型关联:更多使用方法 – 2

时间:2023-02-28 13:58:46浏览次数:50  
标签:Laravel string 22 App 关联 Article id integer find

Laravel 模型关系:多态关联

配套视频地址:https://www.bilibili.com/video/av73028135?p=6

多态一对一

项目:运营人员就一个图片,发布一篇博客或者一篇文章。

articles
    id - integer
    title - string

blogs
    id - integer
    title - string

images
    id - integer
    url - string
    imageable_id - integer
    imageable_type - string

模型

# App\Image
public function imageable()
{
    return $this->morphTo();
}

# App\Blog
public function image()
{
    return $this->morphOne('App\Image', 'imageable');
}

# App\Article
public function image()
{
    return $this->morphOne('App\Image', 'imageable');
}

使用

$article = App\Article::find(1);
$image= $article->image;
$articleWithImages = App\Article::with('image')->find(1);
$articlesWithImages = App\Article::with('image')->find([1, 2]);
$image = App\Image::find(1);
$imageable = $image->imageable;

多态一对多

文章可以接受多条评论,视频也可以接受多条评论。

articles
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

模型

# App\Comment
public function commentable()
{
    return $this->morphTo();
}

# App\Video
public function comments()
{
    return $this->morphMany('App\Comment', 'commentable');
}

# App\Article
public function comments()
{
    return $this->morphMany('App\Comment', 'commentable');
}

使用

$article = App\Article::find(1);

foreach ($article->comments as $comment) {
    //
}
$articleWithComments = App\Article::with('comments')->find(1);

$articlesWithComments = App\Article::with('comments')->find([1, 2]);
$comment = App\Comment::find(1);
$commentable = $comment->commentable;

多态多对多

可以给一篇文章打上多个标签。可以给一个视频打上多个标签。

一个标签可以贴在多个文章上面。一个标签也可以贴在多个视频上。

articles
    id - integer
    name - string

videos
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string

模型

# App\Tag
public function articles()
{
    return $this->morphedByMany('App\Article', 'taggable');
}

public function videos()
{
    return $this->morphedByMany('App\Video', 'taggable');
}
# App\Article
public function tags()
{
    return $this->morphToMany('App\Tag', 'taggable');
}

使用

$video = App\Video::find(1);

foreach ($video->tags as $tag) {
    //
}
$tag = App\Tag::find(1);

foreach ($tag->videos as $video) {
    //
}
$videoWithTags = App\Video::with('tags')->find(1);
$videosWithTags = App\Video::with('tags')->find([1, 2]);

$tagWithVideos = App\Tag::with('videos')->find(1);
$tagsWithVideos = App\Tag::with('videos')->find([1, 2]);

定制

# AppServiceProvider
use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'posts' => 'App\Post',  // posts 是 *_type 字段所存储的值
    'videos' => 'App\Video',
]);

标签:Laravel,string,22,App,关联,Article,id,integer,find
From: https://www.cnblogs.com/fuqian/p/17163941.html

相关文章

  • 26. Laravel 广播 – 公有广播
    Laravel广播–公有广播配套视频地址:https://www.bilibili.com/video/av78577184配置配置驱动"pusher","redis","log","null"//.envnpminstall-......
  • 25. Laravel 事件
    Laravel事件配套视频地址:https://www.bilibili.com/video/av77534496目的:解耦。简介:监听器监听到事件的发生,会执行handler方法。//原始代码publicfunctionreg......
  • 24. Laravel 缓存
    Laravel缓存配套视频地址:https://www.bilibili.com/video/av77035719配置与准备配置文件:config/cache.php,.env可配置内容:1.使用哪个驱动2.驱动的配置......
  • 28. Laravel 内置聊天室
    Laravel内置聊天室配套视频地址:https://www.bilibili.com/video/av80196918配置打开config/app.php中BroadcastServiceProvider注释,即注册广播授权路由。在.e......
  • 27. Laravel 广播 – 私有频道
    Laravel内置聊天室配套视频地址:https://www.bilibili.com/video/av80196918配置打开config/app.php中BroadcastServiceProvider注释,即注册广播授权路由。在.e......
  • 30. Laravel 开发第三方包
    Laravel开发第三方包使用Contracts代替Facades,因为扩展包不能访问Laravel提供的所有测试Facades的辅助函数。自动发现和注册Facades"extra":{"laravel......
  • 29. Laravel 广播系统补充
    Laravel广播系统补充配套视频地址:https://www.bilibili.com/video/av80196918?p=2设置广播名称//默认是事件的类名publicfunctionbroadcastAs(){return's......
  • Laravel 最佳实践
    Laravel最佳实践翻译来源:https://github.com/alexeymezenin/laravel-best-practices单一职责原则不要这样做:publicfunctiongetFullNameAttribute(){if(aut......
  • Laravel 核心概念
    Laravel核心概念配套视频地址:https://www.bilibili.com/video/av76060293核心概念简介、服务容器、服务提供者、facade、contract、生命周期。简介我叫Laravel。......
  • 18. Laravel 模型关系:远程一对一
    Laravel模型关系:远程一对一配套视频地址:https://www.bilibili.com/video/av73028135?p=5一个帖子属于一个作者,该作者就读一所学校。帖子可通过作者访问作者所在的学校......