PHP / Laravel-共享关系的独特结果

oli*_*rbj 5 php laravel eloquent

我正在尝试创建一个Web应用程序,用户可以在其中执行以下操作:

  1. 上传文件
  2. 上载电子邮件(入站电子邮件)

这些文档/电子邮件将被上传到我所谓的“流”中。因此,一个流可以存储许多文档和许多电子邮件。

但是,我进一步尝试这样做,以便我的用户可以根据一组解析规则来解析文档和电子邮件的内容。用户可以创建可以具有许多解析规则的字段

示例: 用户A将新文档上载到名为“我的文档和电子邮件”的流中。用户 为此流定义了以下两个文档字段

  1. 订单参考
  2. 追踪号码

此外,还定义了一些电子邮件字段但是在这种情况下将不使用,因为用户A当前正在上传新文档

  1. 来自名字
  2. 电子邮件主题

上面的所有字段还将具有一些解析规则,这些规则将解析内容(此示例中未显示,因为它不相关)。

如您所见,“我的文档和电子邮件”流同时具有DocumentFieldsEmailFields

这是我当前的设置:

Stream.php

//A stream can have many document fields
public function documentfields()
{
    return $this->hasMany(DocumentField::class);
}
//A stream can have many email fields
public function emailfields()
{
    return $this->hasMany(EmailField::class);
}
Run Code Online (Sandbox Code Playgroud)

Document.php(中有相同的关系Email.php

//A document belongs to a Stream.
public function stream()
{
   return $this->belongsTo(Stream::class);
}

//Get the document fields available for the specific stream.
public function fields()
{
   return $this->stream->documentfields();
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,如何使字段“绑定”到流,但是每个字段Document或每个字段都包含唯一值Email

就像是:

document_fields

id | stream_id          | name             
1  | 1                  | Order Reference 
2  | 1                  | Tracking Number 
Run Code Online (Sandbox Code Playgroud)

document_field_results

id | document_field_id  | document_id  | content
1  | 1                  | 10           | Lorem Ipsum Dolar Amet for document #10    
2  | 2                  | 10           | Some other Lorem Ipsum Dolar Amet for document #10
3  | 1                  | 55           | Lorem Ipsum Dolar Amet for document #55       
4  | 2                  | 55           | Some other Lorem Ipsum Dolar Amet for document #55      
Run Code Online (Sandbox Code Playgroud)

(上载电子邮件/入站电子邮件时的逻辑相同)

因此,每当新文档上载到流(id.1)时,它将“继承”上面的文档字段,但是每个字段的内容都是唯一的(因为每个上载的文档/电子邮件最终将具有不同的内容)。

在这种情况下,正确的关系设置会是什么样?这是正确的方法吗?

Riz*_*lin 1

恕我直言。

streams: id, name

file_categories: id, name。值:文档和电子邮件。这使您可以灵活地添加新类型的文件上传

uploads: id, file_category_id, stream_id ,name

upload_fields: id, stream_id, name

upload_upload_fields: id, upload_id, upload_field_id, content

通过此设置,您无需为电子邮件和文档创建模型。此外,您可以轻松添加新的上传文件类别。

顺便说一句,我已经编程有一段时间了,但刚刚开始回答这些问题。我不知道如何使数据库结构看起来像您的问题一样漂亮。你能给我那个格式的链接吗?

执行

我不确定前端是什么样子。所以我只是按照我的解释去做

流.php:

public function uploadFields(){
    return $this->hasMany(UploadField::class);
}
Run Code Online (Sandbox Code Playgroud)

新文件上传

//First, get the uploadFields based on the stream_id
$uploadFields = Stream::with('uploadFields')->get();

//Second, insert the upload
$upload = new Upload(bla bla bla...); 
$upload->save();
//https://laravel.com/docs/5.8/eloquent#inserts
//Third, use the upload id to fill the upload_upload_fields, or if you prefer the result terms, that's okay
foreach($uploadFields as $field){
    UploadUploadField::insert(['upload_id' => $upload->id, 'upload_file_id' => $field->id, 'content' => 'The Lorem Ipsum, I guess this is from the request']);
}
Run Code Online (Sandbox Code Playgroud)

我想我就是这么做的。它会有点不同,特别是对于 ,content因为它高度依赖于request结构。