2016-09-02 98 views
1

我有一個關於如何將數組傳遞給Laravel 5中的一個控制器的多個視圖的問題,它甚至有可能嗎?我想從一個函數index()傳遞一個數組到index.blade.php和home.blade.php。索引頁面將顯示所有數據,而主頁只顯示一些數據。下面是下面的代碼將數組從一個控制器傳遞給多個視圖? Laravel 5

控制器:

<?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">&laquo;</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">&raquo;</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' => ['[email protected]', $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未定義的變量」。如果有另一種方式請告訴我,謝謝!

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() { 
     // 
    } 

} 

回答

2

您可以使用view composer做到這一點:

View::composer(['index', 'home'], function ($view) { 
    $edisi_list = Edisi::all(); 
    $view->with('edisi_list', $edisi_list); 
}); 
+0

通過使用此,所以我需要做一個新的服務提供商?原諒我,但在哪裏把這個代碼? @alexey –

+0

是的,您應該創建一個服務提供商,您將通過以下鏈接找到示例代碼。如果你不想使用閉包,你還需要創建視圖作曲家類。 –

+1

好的,謝謝你 –

相關問題