Php laravel morphable获取源代码

Php laravel morphable获取源代码,php,laravel,Php,Laravel,我有一个名为documents的变形表,该表存储来自其他表的文件,例如用户产品等 到目前为止,一切都很好,但我还需要在管理面板的文件源信息 示例 doc 1 => product ABC doc 2 => user XYZ 我需要将此产品ABC和用户XYZ作为每个文档的来源我该怎么做? 代码 doc 1 => product ABC doc 2 => user XYZ 控制器 public function index() { $documents = Doc

我有一个名为
documents
的变形表,该表存储来自其他表的文件,例如
用户
产品

到目前为止,一切都很好,但我还需要在管理面板的文件源信息

示例

doc 1  => product ABC
doc 2 => user XYZ
我需要将此
产品ABC
用户XYZ
作为每个文档的来源我该怎么做?

代码

doc 1  => product ABC
doc 2 => user XYZ
控制器

public function index()
{
  $documents = Document::orderby('id', 'desc')->get();
  return view('admin.documents.index', compact('documents'));
}
blade

@foreach($documents as $document)
  {{$document->name}}
  // here need source name
@endforeach
有什么想法吗?

更新 模型关系

产品型号

public function documents()
{
  return $this->morphMany(Document::class, 'documentable');
}
protected $fillable = [
  'documentable_id', 'documentable_type', 'file', 'download_count',
];

public function documentable()
{
  return $this->morphTo();
}
文档模型

public function documents()
{
  return $this->morphMany(Document::class, 'documentable');
}
protected $fillable = [
  'documentable_id', 'documentable_type', 'file', 'download_count',
];

public function documentable()
{
  return $this->morphTo();
}

你能发布你的文档模型关系吗?@designtocode我很抱歉bro在大量的工作中迷失了方向:)这里我为你更新了我的问题。试试
$Document->documentable
。要检查它是产品还是用户,请检查
$document->documentable\u type
的值。好的,我会尝试让您知道