Friday, 17 August 2018

Simple CRUD (Create Read Update Delete) Laravel 5.6

Kalau belum pernah install laravel lihat di sini

Konfigurasi Database
buka file .env edit bagian

DB_DATABASE=blog
DB_USERNAME=zain
DB_PASSWORD=123

blog => nama database
zain => username database
123 => password database

 

Membuat Migration Database
Migration adalah sebuah fasilitas dilaravel digunakan untuk mempermudah ketika ada perubahan dalam database. Schema Builder digunakan untuk
membuat sebuah skema database.

Buka terminal/cmd kemudian ketik:

php artisan make:migration create_articles_table

 

Buka file create_articles_table letaknya di database/migrations
tabahkan kode ini:

$table->string('title');
$table->text('description');

 

Contoh lengkapnya seperti di bawah ini:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration

{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

 

Buka terminal/cmd kemudian ketik:

php artisan migrate

 

Membuat Model

Model merupakan penghubung setiap alur program yang terhubung dengan database.
Buka terminal/cmd kemudian ketik:

php artisan make:model Article

Buka file model article letaknya di app/

 

Tambahkan kode:

protected $fillable = [
    'title', 'description'
];

 

Kode lengkapnya seperti ini:

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Article extends Model

{
    protected $fillable = [
        'title', 'description'
    ];
}

 

Buka routes/web.php
tambahkan kode ini:

Route::resource('article', 'ArticleController');

 

Membuat Controller
Buka terminal/cmd kemudian ketik:
php artisan make:controller ArticleController –resource

Buka file ArticleController.php letaknya di app/Http/Controllers/ArticleController.php
Kemudian edit kodenya seperti di bawah ini:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;

class ArticleController extends Controller

{
    public function index()
    {
        $data = Article::all();
        return view('articles.index', ['data' => $data]);
    }
    public function create()
    {
        return view('articles.create');
    }
    public function store(Request $request)
    {
        Article::create($request->all());
        return redirect('article');
    }
    public function show($id)
    {
        $data = Article::find($id);
        return view('articles.show', ['data' => $data]);
    }
    public function edit($id)
    {
        $data = Article::find($id);
        return view('articles.edit', ['data' => $data]);
    }
    public function update(Request $request, $id)
    {
        Article::find($id)->update($request->all());
        return redirect('article');
    }
    public function destroy($id)
    {
        Article::find($id)->delete();
        return back();
    }
}

 

Membuat View

Buatlah struktur letak file view seperti di bawah ini:

Sekarang buka file app.blade.php (letaknya di resources/views/layouts/app.blade.php)
Kemudian ketik kode di bawah ini:

<html>
<head>
    <title>Article</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <script src="{{ asset('js/app.js') }}" defer></script>
</head>

<body>

<div class="container mt-5">
    @yield('content')
</div>

</body>
</html>

 

Sekarang buka file index.blade.php (letaknya di resources/views/articles/index.blade.php)
kemudian ketik kode di bawah ini:

@extends('layouts.app')
@section('content')

    <a href="{{ url('article/create') }}" class="btn btn-primary">Tambah Artikel</a>
    <table class="table mt-3">
        <thead>
        <th>
            Title
        </th>
        <th>
            Description
        </th>
        <th>
            Date
        </th>
        <th>
            Action
        </th>
        </thead>
        <tbody>
        @foreach($data as $d)
            <tr>
                <td>
                    {{ $d->title }}
                </td>
                <td>
                    {{ str_limit($d->description, 80) }}
                </td>
                <td>
                    {{ $d->created_at }}
                </td>
                <td>
                    <form action="{{ url('article/'.$d->id) }}" method="post">
                        {{ csrf_field() }}
                        {{ method_field('DELETE') }}
                        <a href="{{ url('article/'.$d->id) }}" class="btn btn-sm btn-primary">Show</a>
                        <a href="{{ url('article/'.$d->id.'/edit') }}" class=" btn btn-sm btn-primary">Edit</a>
                        <button class="btn btn-sm btn-danger" type="submit" onclick="return confirm('Yakin ingin menghapus data?')">Delete</button>
                    </form>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>

@endsection

 

Sekarang buka file create.blade.php (letaknya di resources/views/articles/create.blade.php)
Kemudian ketik kode di bawah ini:

@extends('layouts.app')
@section('content')

    <form action="{{ url('article') }}" method="post">
        {{ csrf_field() }}
        {{ method_field('POST') }}
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" placeholder="Title">
        </div>
        <div class="form-group">
            <label for="description">Description</label>
            <textarea class="form-control" id="description" name="description" rows="3" placeholder="Description"></textarea>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>

@endsection

 

Sekarang buka file edit.blade.php (letaknya di resources/views/articles/edit.blade.php)
Kemudian ketik kode di bawah ini:

@extends('layouts.app')
@section('content')

    <form action="{{ url('article/'.$data->id) }}" method="post">
        {{ csrf_field() }}
        {{ method_field('PUT') }}
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" value="{{ $data->title }}" placeholder="Title">
        </div>
        <div class="form-group">
            <label for="description">Description</label>
            <textarea class="form-control" id="description" rows="3" placeholder="Description">{{ $data->description }}</textarea>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>

@endsection

 

Sekarang buka file show.blade.php (letaknya di resources/views/articles/show.blade.php)
Kemudian ketik kode di bawah ini:

@extends('layouts.app')
@section('content')

    <h4>{{ $data->title }}</h4><br>
    <p>{{ $data->description }}</p>

@endsection

 

 

 

 

 


Share :