duanjucong3124 2019-06-05 09:26 采纳率: 100%
浏览 59

动态Laravel索引布局

I create a new version of laravel, and in that project there are different kind of models with different kind of attributes,

for those models I want to create a dynamic index page like this,

I include a layout file and pass variables to it such as records, specific column title and value for each of them, but the problem here is that for example and article has multiple relationships like category, author or ...

or for edit route of articles we should pass record to it for route model binding

here is my layout file

@if ($records->count())
               <table>
                    <thead class="text-center">
                         <tr>
                              @foreach($fields as $field)
                              <th scope="col">{{ $field[0] }}</th>
                              @endforeach
                         </tr>
                    </thead>
                    <tbody class="text-center">
                         @foreach ($records as $record)
                         <tr>
                              @foreach($fields as $field)
                                   <td>{{ $record->{$field[1]} }}</td>
                              @endforeach
                              <td>
                                   <a href="{{ route($paths['edit'], $record) }}"><button type="button" class="btn btn-sm btn-primary" rel="tooltip" data-placement="top" title="Edit"><i class="far fa-edit"></i></button></a>
                              </td>
                         </tr>
                         @endforeach
                    </tbody>
               </table>
@endif

and in the other hand in the specific index file for each model we have this code

@include('layouts.index', [
 'title' => 'Articles Index',
 'records' => $articles,
 'fields' => [
      [ "Article Title", "title" ],
      [ "Category", $record->category->title],
      [ "View", "view"],
 ],
 'paths' => [
      'edit' => "{{ route('articles.edit', @$record) }}",
  ],
])

records variable is the collection for model that passes from controller to view fields have array inside it self first item is column title that shown as table thead and second one is the field that should be shown in every row of table for each row of records,

but the problem is that for category we should use the $record variable that will used in 2nd foreach of layout file, but in the specific index of models its not found!

I need a way that blade load this variable in the layout file, not the specific index view of model,

a way that pass code from one blade to another one!!

Hope you help me find a way Regards

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥15 MCNP里如何定义多个源?
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services