我有一个关于如何从Laravel 5中的一个控制器将数组传递给多个视图的问题,是否可能?我想从一个函数索引()传递一个数组到index.blade.php和home.blade.php。索引页面将显示所有数据,而主页仅显示一些数据。以下是代码
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\EdisiRequest;
use App\Edisi;
use Session;
use Storage;
class EdisiController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$edisi_list = Edisi::all();
return view('edisi/index', compact('edisi_list'));
}
}
home.blade.php
@extends('template')
@section('main')
<div class="container sitecontainer bgw">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12 m22 single-post">
@if (count($edisi_list) > 0)
<div class="reviewlist review-posts">
<?php foreach ($edisi_list as $edisi): ?>
<div class="post-review col-md-4 col-sm-12">
<div class="post-media entry">
<a href="#" title="">
<img src="{{ asset('fotoupload/' . $edisi->cover) }}" alt="fotoupload/coveredisi.png" class="img-responsive" style="height: 480px; width: 1200px;">
</a>
</div><!-- end media -->
<div class="post-title">
<h4 style="text-align: center;"><a href="#">{{ $edisi->judul }}</a></h4>
</div><!-- end post-title -->
</div><!-- end post-review -->
<?php endforeach ?>
</div><!-- end review-posts -->
@else
<p>Tidak ada data edisi.</p>
@endif
</div><!-- end col -->
</div><!-- end row -->
<div class="row">
<div class="pagination-wrapper text-center">
<nav>
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</div><!-- end container -->
@stop
index.blade.php
@extends('template')
@section('main')
<div class="container sitecontainer bgw">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12 m22 single-post">
<div id="siswa">
<h2>Daftar Edisi</h2>
@if (count($edisi_list) > 0)
<table class="table">
<thead>
<tr>
<th>Judul</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($edisi_list as $edisi): ?>
<tr>
<td>{{ $edisi->judul }}</td>
<td>
<div class="box-button">
{{ link_to('edisi/' . $edisi->id, 'Detail', ['class' => 'btn btn-success btn-sm']) }}
</div>
<div class="box-button">
{{ link_to('edisi/' . $edisi->id . '/edit', 'Edit', ['class' => 'btn btn-warning btn-sm']) }}
</div>
<div class="box-button">
{!! Form::open(['method' => 'DELETE', 'action' => ['EdisiController@destroy', $edisi->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!}
{!! Form::close() !!}
</div>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
@else
<p>Tidak ada data edisi.</p>
@endif
<div class="tombol-nav">
<a href="edisi/create" class="btn btn-primary">Tambah Edisi</a>
</div>
</div> <!-- / #jurnal -->
</div><!-- end col -->
</div><!-- end row -->
</div><!-- end container -->
@stop
我尝试为主页制作另一个控制器,但仍然无法正常工作,它总是显示错误“$ edisi_list undefined variable”。如果还有其他方法请告诉我,谢谢!
EdisiServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Edisi;
class EdisiServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot() {
View::composer(['edisi/index', 'pages/home'], function ($view) {
$edisi_list = Edisi::all();
$view->with('edisi_list', $edisi_list);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register() {
//
}
}
答案 0 :(得分:3)
您可以使用view composer执行此操作:
View::composer(['index', 'home'], function ($view) {
$edisi_list = Edisi::all();
$view->with('edisi_list', $edisi_list);
});
答案 1 :(得分:0)
对于任何其他使用View Composer传递数组的人,使用compact
传递数组将不起作用。
您必须直接发送:
在自定义作曲家中(在我的情况下为app/Http/View/Composers/UserInfoComposer.php
)
public function compose( View $view ) {
$allUserInfo = array();
$generalFunctions = new GeneralFunctions();
$currentUserID = $generalFunctions->getCurrentUserID();
$currentUserName = $generalFunctions->getCurrentUserName();
$currentUserEmail = $generalFunctions->getCurrentUserEmail();
$currentUserProfilePic = $generalFunctions->getCurrentProfilePic();
$allUserInfo['currentUserID'] = $currentUserID;
$allUserInfo['currentUserName'] = $currentUserName;
$allUserInfo['currentUserEmail'] = $currentUserEmail;
$allUserInfo['currentUserProfilePic'] = $currentUserProfilePic;
$view->with( 'allUserInfo', $allUserInfo );
//NOT: $view->with( 'allUserInfo', compact('allUserInfo'));
}
创建视图撰写器的参考:LINK