Owl Admin 中文文档


模型关联展示数据

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

页面列表

ITEM_HTML