Cara Membuat CRUD Di Laravel 8

 

Cara Membuat CRUD Di Laravel 8


Halo guys ketemu lagi sama saya, sebelum nya kira membahas tentang Routing Pada Laravel kali ini saya mau bahas Cara Membuat CRUD di Laravel  8, ok langsung aja kita ke pembahasan.....

-Install Laravel 8


Pertama kita membutuhkan project baru Laravel 8, jalankan perintah berikut di command prompt atau terminal untuk membuat nya :

composer create-project --prefer-dist laravel/laravel blog

-Setting Database

di tahap kedua kita harus setting database pada .env di dalam project laravel yang kita buat tadi, dan masukkan code berikut jika kalian menggunakan MySQL :

.env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(abouts)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

-Membuat Migrasi

Kita akan membuat CRUD untuk about , jadi kita harus membuat migrasi untuk "abouts" table menggunakan perintah di laravel 8 , jadi oeruntah pertama :

php artisan make:migration create_abouts_table --create=abouts


setelah menjalankan perintah kalian cari folder "database/migration" dan masukkan code berikut ke dalam file migrasi untuk create abouts table.

<?php

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

class CreateAboutsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('abouts', function (Blueprint $table) {
$table->id();
$table->string('nama');
$table->text('keterangan');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('abouts');
}
}
sekarang migrate dengan menjalankan code berikut :

php artisan migrate


-Route

sekarang, kita buat route untuk CRUD about. Buka file "routes/web.php" kalian dan tambahkan route berikut : 

Route::resource('abouts', AboutController::class);

-Controller dan Model 

di step ini, ktia harus membuat controller baru dengan nama AboutController. Jalankan perintah berikut untuk membuat controller :

php artisan make:controller AboutController --resource --model=About


setelah perintah di atas dijalankan maka akan terbuat file baru di :
"app/Http/Controllers/AboutController.php".

di dalam controller ini kita akan membuat 7 method yaitu :
1.index()
2.create()
3.store()
4.show()
5.edit()
6.update()
7.destroy()

Copy code berikut dan masukkan ke AboutController.php :

<?php

namespace App\Http\Controllers;

use App\Models\About;
use Illuminate\Http\Request;

class AboutController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$abouts = About::latest()->paginate(5);

return view('abouts.index',compact('abouts'))
->with('i', (request()->input('page', 1) - 1) * 5);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('abouts.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'nama' => 'required',
'keterangan' => 'required',
]);

About::create($request->all());

return redirect()->route('abouts.index')
->with('success','created successfully.');
}

/**
* Display the specified resource.
*
* @param \App\About $about
* @return \Illuminate\Http\Response
*/
public function show(About $about)
{
return view('abouts.show',compact('about'));
}

/**
* Show the form for editing the specified resource.
*
* @param \App\About $about
* @return \Illuminate\Http\Response
*/
public function edit(About $about)
{
return view('abouts.edit',compact('about'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\About $about
* @return \Illuminate\Http\Response
*/
public function update(Request $request, About $about)
{
$request->validate([
'nama' => 'required',
'keterangan' => 'required',
]);

$about->update($request->all());

return redirect()->route('abouts.index')
->with('success','updated successfully');
}

/**
* Remove the specified resource from storage.
*
* @param \App\About $about
* @return \Illuminate\Http\Response
*/
public function destroy(About $about)
{
$about->delete();

return redirect()->route('abouts.index')
->with('success','deleted successfully');
}
}
Setelah itu kalian buka "app/Http/Models/About.php" dan masukkan code berikut :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class About extends Model
{
use HasFactory;

protected $fillable = [
'nama', 'keterangan'
];
}

-Blade Files

di step terakhir kita akan membuat blade file di dalam folder resources/views , buatlah folder baru di dalam views dengan nama "abouts" dan buat 5 file berikut :
1) layout.blade.php
2) index.blade.php
3) create.blade.php
4) edit.blade.php
5) show.blade.php
Copy code berikut kedalam file :

resources/views/abouts/layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 CRUD Application - ItSolutionStuff.com</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>

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

</body>
</html>

resources/views/abouts/index.blade.php

@extends('abouts.layout')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example from scratch - ItSolutionStuff.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('abouts.create') }}"> Create New</a>
</div>
</div>
</div>

@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif

<table class="table table-bordered">
<tr>
<th>No</th>
<th>Nama</th>
<th>Keterangan</th>
<th width="280px">Action</th>
</tr>
@foreach ($abouts as $about)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $about->name }}</td>
<td>{{ $about->detail }}</td>
<td>
<form action="{{ route('abouts.destroy',$about->id) }}" method="POST">

<a class="btn btn-info" href="{{ route('abouts.show',$about->id) }}">Show</a>

<a class="btn btn-primary" href="{{ route('abouts.edit',$about->id) }}">Edit</a>

@csrf
@method('DELETE')

<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>

{!! $abouts->links() !!}

@endsection

resources/views/abouts/create.blade.php

@extends('abouts.layout')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('abouts.index') }}"> Back</a>
</div>
</div>
</div>

@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<form action="{{ route('abouts.store') }}" method="POST">
@csrf

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nama:</strong>
<input type="text" name="nama" class="form-control" placeholder="Nama">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Keterangan:</strong>
<textarea class="form-control" style="height:150px" name="keterangan" placeholder="Keterangan"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

</form>
@endsection

resources/views/abouts/edit.blade.php

@extends('abouts.layout')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('abouts.index') }}"> Back</a>
</div>
</div>
</div>

@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<form action="{{ route('abouts.update',$about->id) }}" method="POST">
@csrf
@method('PUT')

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nama:</strong>
<input type="text" name="name" value="{{ $about->nama }}" class="form-control" placeholder="Nama">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Keterangan:</strong>
<textarea class="form-control" style="height:150px" name="keterangan" placeholder="Keterangan">{{ $about->keterangan }}</textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

</form>
@endsection

resources/views/abouts/show.blade.php

@extends('abouts.layout')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show </h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('abouts.index') }}"> Back</a>
</div>
</div>
</div>

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nama:</strong>
{{ $about->nama}}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Keterangan:</strong>
{{ $about->keterangan}}
</div>
</div>
</div>
@endsection
Selesai , sekarang jalankan perintah berikut :

php artisan serve

Buka URL berikut di browser kalian :

http://localhost:8000/abouts

Sekian dari saya mohon maaf jika ada kesalahan , sampai jumpa di post berikutnya :D

Sumber : https://www.itsolutionstuff.com/post/laravel-8-crud-application-tutorial-for-beginnersexample.html

Comments

Popular posts from this blog

Membuat Aplikasi Belanja Sederhana Menggunakan Python

Cara Mengirim Email Menggunakan Python

4 Bahasa Pemrograman Terbaik Untuk Website Back-End