模型关联展示数据
<h3>假设你定义了这么一个关联</h3>
<pre><code class="language-php">public function branch()
{
return $this-&gt;belongsTo(Branch::class);
}</code></pre>
<p><em>如果你连模型关联都没玩明白, 建议先通读并背诵 <a href="https://learnku.com/docs/laravel/9.x/eloquent-relationships/12252">文档</a> ~</em></p>
<p><br></p>
<h3>现在你需要在列表中或者详情中回显</h3>
<h5>更改 Service 中的查询</h5>
<pre><code class="language-php">// 列表中
public function listQuery()
{
$model = $this-&gt;getModel();
// 原本的查询长这样
// return $this-&gt;query()-&gt;orderByDesc($model-&gt;getUpdatedAtColumn());
// 现在你需要改成这样, 预加载 branch 关联
return $this-&gt;query()-&gt;with('branch')-&gt;orderByDesc($model-&gt;getUpdatedAtColumn());
}
// 详情中
public function getDetail($id)
{
// 原本的查询长这样
// return $this-&gt;query()-&gt;find($id);
// 现在你需要改成这样, 预加载 branch 关联
return $this-&gt;query()-&gt;with('branch')-&gt;find($id);
}</code></pre>
<p><em>更改查询后, 你的数据将从一维变成二维</em></p>
<pre><code class="language-php">// 直观一点看
// 原本数据长这样
[
'id' =&gt; 1,
'name' =&gt; '张三',
'branch_id' =&gt; 1,
]
// 现在数据长这样
[
'id' =&gt; 1,
'name' =&gt; '张三',
'branch_id' =&gt; 1,
'branch' =&gt; [
'id' =&gt; 1,
'name' =&gt; '总部',
],
]</code></pre>
<p><em>如果你连数据结构为什么变成这样都有点蒙圈, 建议先通读并背诵 <a href="https://learnku.com/docs/laravel/9.x/eloquent-relationships/12252#012e7e">文档</a> ~</em></p>
<p><br></p>
<h3>现在你需要在列表中或者详情中回显</h3>
<pre><code class="language-php">// 列表中
// ...
// 你现在可以通过 . 的方式取到多个层级的数据
TableColumn::make()-&gt;name('branch.name')-&gt;label('所属分公司'),
// ...
// 详情中
// ...
// 你现在可以通过 . 的方式取到多个层级的数据
TextControl::make()-&gt;static(true)-&gt;name('branch.name')-&gt;label('所属分公司'),
// ...</code></pre>
<p><br></p>
<h3>为什么可以这么玩?</h3>
<p>这里你需要先了解一下 <code>amis</code> 中的 <strong>数据域</strong> 概念:
<a href="https://aisuda.bce.baidu.com/amis/zh-CN/docs/concepts/datascope-and-datachain"><a href="https://aisuda.bce.baidu.com/amis/zh-CN/docs/concepts/datascope-and-datachain">https://aisuda.bce.baidu.com/amis/zh-CN/docs/concepts/datascope-and-datachain</a></a></p>