Laravel 获取部分数据作为数组
Laravel get sections data as an array
假设我有一个包含以下内容的视图文件 view/admin/users.blade.php
:
@extends('layouts.master')
@section('head')
<link rel="stylesheet" href="/css/style1.css" />
<link rel="stylesheet" href="/css/style2.css" />
@endsection
@section('content')
<div class="row">
<table>
<thead>
<tr>
<th>ID</th>
<th>email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
@section('scripts')
<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>
@endsection
有什么方法可以将节(每个节的数据)作为数组获取吗?像这样:
[
'head' => '<link rel="stylesheet" href="/css/style1.css" /><link rel="stylesheet" href="/css/style2.css" />',
'content' => '<div class="row"><table><thead><tr><th>ID</th><th>email</th></tr></thead><tbody>@foreach($users as $user)<tr><td>{{ $user->id }}</td><td>{{ $user->email }}</td></tr>@endforeach</tbody></table></div>',
'scripts' => '<script src="/js/script1.js"></script><script src="/js/script2.js"></script>'
]
使用renderSections
方法。
$view = view('blade.template')->with(compact('something'));
dd($view->renderSections());
哪个returns assoc array keyed by its section name.
假设我有一个包含以下内容的视图文件 view/admin/users.blade.php
:
@extends('layouts.master')
@section('head')
<link rel="stylesheet" href="/css/style1.css" />
<link rel="stylesheet" href="/css/style2.css" />
@endsection
@section('content')
<div class="row">
<table>
<thead>
<tr>
<th>ID</th>
<th>email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
@section('scripts')
<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>
@endsection
有什么方法可以将节(每个节的数据)作为数组获取吗?像这样:
[
'head' => '<link rel="stylesheet" href="/css/style1.css" /><link rel="stylesheet" href="/css/style2.css" />',
'content' => '<div class="row"><table><thead><tr><th>ID</th><th>email</th></tr></thead><tbody>@foreach($users as $user)<tr><td>{{ $user->id }}</td><td>{{ $user->email }}</td></tr>@endforeach</tbody></table></div>',
'scripts' => '<script src="/js/script1.js"></script><script src="/js/script2.js"></script>'
]
使用renderSections
方法。
$view = view('blade.template')->with(compact('something'));
dd($view->renderSections());
哪个returns assoc array keyed by its section name.