使用雄辩的多态关系对Laravel中的数据进行分类 [英] Using Eloquent polymorphic relationships to categorise data in Laravel

查看:102
本文介绍了使用雄辩的多态关系对Laravel中的数据进行分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个类别表,还有多个表,包括需要分类的服务和文章。为了方便起见,我想为此数据使用多态模型。



创建一个类别以供服务或文章使用。一旦将表定义为服务表,例如,它不会被显示为添加文章的可能类别。



这也适用于列表。我将有一个文章菜单和一个服务菜单,每个菜单都将列出文章或服务类别,无论是空白还是包含各自的类型。



例如,服务类别可用于许多服务。同样,一个服务可以有很多类别,但只能有服务类别。



就目前而言,我的数据库可能不足。我没有使用数据透视表,但是当我尝试使用一个数据表时,它不允许我只选择文章类别或服务类别;实际上,两者之间根本无法区别。





基本上,我需要能够:




  • 获取服务可用的所有类别列表(可能 where('categorizable_type','App\Service')?)

  • 获取文章可用的所有类别列表(可能 where('categorizable_type','App \Article')?)

  • 获取类别中所有文章/服务的列表

  • 获取文章/服务使用的所有类别的列表



在这种理想情况下,我可以很容易地向类别添加新的(或现有的)文章/服务:

  $ category = App\Category: :(1)查找; 
$ article = new App \Article();
$ article-> title ='这是文章标题'。
$ category-> articles() - > save($ article);

我可以轻松地向文章/服务添加新的(或现有的)类别:

  $ article = App \Article :: find(1); 
$ category = new App \Category();
$ category-> title ='这是一个类别标题'。
$ article-> categories() - > save($ category);


解决方案

嗯,你的数据库和你的模型定义看起来不错,以及您为文章添加类别的代码。但是,您将代码添加到类别中有一点点。



首先,没有 $ category-> articles() 方法。要访问相关对象,您可以使用定义的关系: $ category->可分类()。此外,关系属性 $ category->可分类将包含加载的相关对象,它将自动为文章服务对象,具体取决于相关内容。



其次,这是 morphTo()关系的一面;它的行为类似于 belongsTo(),并具有类似的方法。例如,没有 save()方法,但您有 associate()方法。这意味着您必须先创建您的文章,然后将其与该类别相关联。另外, associate()不会自动保存,所以您也需要调用。

  $ article = new App \Article(); 
$ article-> title ='这是文章标题'。
$ article-> save();

$ category = App \Category :: find(1);
$ category->可分类() - > associate($ article);
$ category-> save();

//显示关系属性的使用
$ related = $ category->可分类;
echo get_class($ related);


In my application, I have a categories table, and multiple tables including services and articles that need to be categorised. For convenience, I want to use a polymorphic model for this data.

A category is created to be used by either services or articles. Once a table has been defined as a services table, for example, it is not going to be displayed as a possible category to add articles to.

This also applies to listing. I will have a menu for articles, and a menu for services, and each of these will list just article or service categories, whether they be empty or whether they contain their respective types.

A service category, for example, can be used for many services. Similarly, a service can have many categories - but only service categories.

As it stands, my database is possibly insufficient. I have not used a pivot-table, but when I tried to use one, it didn't allow me to select just article categories or service categories; in fact, it couldn't differ between the two at all.

Basically, I need to be able to:

  • Get a list of all categories usable by services (possibly where('categorizable_type', 'App\Service')?)
  • Get a list of all categories usable by articles (possibly where('categorizable_type', 'App\Article')?)
  • Get a list of all articles/services in a category
  • Get a list of all categories used by an article/service

In this ideal situation, I can easily add new (or existing) articles/services to categories, too:

$category = App\Category::find(1);
$article = new App\Article();
$article->title = 'This is an article title.';
$category->articles()->save($article);

And I can easily add new (or existing) categories to articles/services:

$article = App\Article::find(1);
$category = new App\Category();
$category->title = 'This is a category title.';
$article->categories()->save($category);

解决方案

Well, your database and your model definitions look fine, as well as your code for adding a category to an article. However, your code for adding an article to a category is a little off.

First, there is no $category->articles() method. To access the related objects, you use your defined relationship: $category->categorizable(). Also, the relationship attribute, $category->categorizable will contain the loaded related object, and it will automatically either be an Article or a Service object, depending on what is related.

Second, this is the morphTo() side of the relationship; it behaves like the belongsTo(), and has similar methods. For example, there is no save() method, but you have the associate() method. This means you have to create your article first, and then associate it with the category. Also, associate() does not automatically save, so you need to call that, as well.

$article = new App\Article();
$article->title = 'This is an article title.';
$article->save();

$category = App\Category::find(1);
$category->categorizable()->associate($article);
$category->save();

// showing use of relationship attribute
$related = $category->categorizable;
echo get_class($related);

这篇关于使用雄辩的多态关系对Laravel中的数据进行分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆