samedi 3 septembre 2016

Laravel 5 querying related data

I have a problem on how to show related data from two tables. I got table Edisi (Edition in English) and table Jurnal (Journal in English). Both tables related with one-to-many relationship, table Edisi supposed to has many Jurnal data and I wanted to know how to show it in Laravel. For example, I have an Edition available, when I click on it it will show the Journals list that the Edition has. Below is the table structure :

Edisi table :

class CreateTableEdisi extends Migration
{
    public function up()
    {
        Schema::create('edisi', function (Blueprint $table) {
            $table->increments('id');
            $table->string('judul')->unique();
            $table->text('cover')->nullable();
            $table->timestamps();
        });

        //Set FK di kolom id_edisi di table Jurnal
        Schema::table('jurnal', function(Blueprint $table) {
            $table->foreign('id_edisi')->references('id')->on('edisi')->onDelete('cascade')->onUpdate('cascade');
        });
    }

    public function down()
    {
        Schema::table('jurnal', function(Blueprint $table) { 
            $table->dropForeign('jurnal_id_edisi_foreign');
        });

        Schema::drop('edisi');
    }
}

Jurnal table :

class CreateTableJurnal extends Migration
{
    public function up()
    {
        Schema::create('jurnal', function (Blueprint $table) {
            $table->increments('id');
            $table->string('judul', 200);
            $table->string('penulis');
            $table->text('abstrak');
            $table->text('file');
            $table->integer('id_edisi')->unsigned();
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop('jurnal');
    }
}

Controller :

public function show(Edisi $edisi)
    {
        $jurnal_list = DB::table('jurnal')->where('id_edisi', '=', $edisi)->get();
        return view('edisi/show', compact('edisi', 'jurnal_list'));
    }

View :

@extends('template')

@section('main')
<div class="container sitecontainer single-wrapper bgw">
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12 m22">
            <div class="widget searchwidget joblist">

                <div class="large-widget m30">
                    <div class="post row clearfix">
                        <div class="col-md-4">
                            <div class="post-media">
                                <img alt="" src="" class="img-responsive">
                                <a class="btn btn-primary btn-block"></a>  
                            </div>
                        </div>

                        <div class="col-md-8">
                            <div id="siswa">
                                <h2>Daftar Jurnal</h2>

                                @if (count($jurnal_list) > 0)
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>Judul</th>
                                            <th>Penulis</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($jurnal_list as $jurnal): ?>
                                            <tr>
                                                <td></td>
                                                <td></td>
                                                <td>
                                                    <div class="box-button">
                                                        
                                                    </div>
                                                    <div class="box-button">
                                                        
                                                    </div>
                                                    <div class="box-button">
                                                        {!! Form::open(['method' => 'DELETE', 'action' => ['JurnalController@destroy', $jurnal->id]]) !!}
                                                        {!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!}
                                                        {!! Form::close() !!}
                                                    </div>
                                                </td>
                                            </tr>
                                        <?php endforeach ?>
                                    </tbody>
                                </table>
                                @else
                                <p>Tidak ada data jurnal.</p>
                                @endif

                                <div class="tombol-nav">
                                    <a href="../jurnal/create" class="btn btn-primary">Tambah Jurnal</a>
                                </div>
                            </div> <!-- / #jurnal -->
                        </div>
                    </div><!-- end post -->
                </div><!-- end large-widget -->
            </div><!-- end widget -->
        </div><!-- end col -->
    </div><!-- end row -->
</div><!-- end container -->
@stop

I'm new to Laravel and I'm stuck here. I tried everything I could (look at the controller) and still the journal data won't show up. Thanks for your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire