将数据插入相关模型

时间:2016-09-03 14:12:25

标签: laravel laravel-5

如何将新数据添加到相关表中?我有两张桌子,第一张是Edisi(英文版)表和Jurnal(英文杂志)表。 edisi表将包含许多jurnal数据,或者在英语中,Edition表将包含许多Journals数据。我的问题是,创建和保存的方法怎么样?我已经创建了该功能,但它无法正常工作。

Edisi表:

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');
    }
}

控制器:

public function create()
    {
        return view('jurnal/create');
    }

    public function store(JurnalRequest $request)
    {
        $input = $request->all();

        //Input PDF
        if ($request->hasFile('file')) {
            $input['file'] = $this->uploadPDF($request);
        }

        //Insert data jurnal
        $jurnal = Jurnal::create($input);

        return redirect('jurnal');
    }

显示视图:

@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="{{ asset('fotoupload/' . $edisi->cover) }}" class="img-responsive">
                                <a class="btn btn-primary btn-block">{{ $edisi->judul }}</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>{{ $jurnal->judul }}</td>
                                                <td>{{ $jurnal->penulis }}</td>
                                                <td>
                                                    <div class="box-button">
                                                        {{ link_to('jurnal/' . $jurnal->id, 'Detail', ['class' => 'btn btn-success btn-sm']) }}
                                                    </div>
                                                    <div class="box-button">
                                                        {{ link_to('jurnal/' . $jurnal->id . '/edit', 'Edit', ['class' => 'btn btn-warning btn-sm']) }}
                                                    </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>No Journals yet.</p>
                                @endif

                                <div class="tombol-nav">
                                    <a href="../jurnal/create" class="btn btn-primary">Add new Journal to this edition</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

我是Laravel的新手,我被困在这里。我尽我所能(看看控制器),当我尝试在所选版本中添加日志数据时,它显示错误约束失败。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

首先将“jurnal”表格中的$table->integer('id_edisi')->unsigned();更改为$table->integer('edisi_id')->unsigned();

然后将以下方法添加到“Jurnal”模型

public function edisi()
{
    return $this->belongsTo('App\Edisi');
}

如果您不想像我之前所示更改jurnal表,请按照以下方法修改上述方法

public function edisi()
{
    return $this->belongsTo('App\Edisi', 'id_edisi');
}

然后在控制器中

$jurnal_list = Jurnal::with('edisi')->get();

如果您想从“edisi”访问“jurnal”,请在“Edisi”模型中添加以下方法

public function jurnals()
{
    return $this->hasMany('App\Jurnal');
}

或未经修改的jurnals表。

public function jurnals()
{
    return $this->hasMany('App\Jurnal', 'id_edisi');
}

然后在控制器中

$jurnal_list = Edisi::with('jurnals')->get();

注意:确保将$table变量添加到“Jurnal”和“Edisi”模型,因为通常表名需要像s(例如: - 模型:帖子表:帖子,模型:评论表:评论)