vendredi 2 septembre 2016

Passing an array to more than one view from one controller ? Laravel 5

I have a question about how to pass an array to more than one view from one controller in Laravel 5, is it even possible ? I want to pass an array to index.blade.php and to home.blade.php from one function index(). The index page will show all of the data while the home page will only show a few. Here are the code below

Controller :

<?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="" 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="#"></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></td>
                                <td>
                                    <div class="box-button">
                                        
                                    </div>
                                    <div class="box-button">
                                        
                                    </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

I tried by making another controller for the home page, but still not working, it always show error "$edisi_list undefined variable". If there is another way to that please show me, thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire