MHSyahfarel PWF CRUDLaravel

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

PEMBUATAN CRUD LARAVEL

PEMROGRAMAN WEB FRAMWORK


Dosen Pengampu : Rudy Asrianto, M. Kom

Disusun oleh :

Muhammad Haikal Syahfarel

(210402114)

PROGRAM STUDI SISTEM INFORMASI

FAKULTAS ILMU KOMPUTER

UNIVERSITAS MUHAMMADIYAH RIAU

2023
1. CREATE

Kodingan View Create :

@extends('front.panel')

@section('content')
<div class="right_col" role="main">
<div class>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="x_panel">
<div class="x_title">
<h2>Form Add <small>Produk</small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('produk.store') }}"
method="post" enctype="multipart/form-data">
@csrf
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Nama<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="text" name="nama" id="nama"
class="form-control" placeholder="Nama" required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Harga<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="text" name="harga" id="harga"
class="form-control" placeholder="Harga" required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Stok<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="number" name="stok" id="stok"
class="form-control" placeholder="Stok" required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Keterangan<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="text" name="keterangan"
id="keterangan" class="form-control" placeholder="Keterangan" required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-3
label-align">Foto<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 text-center">
<div class="custom-file">
<input type="file" name="foto"
id="foto" class="custom-file-input" required>
<label class="custom-file-label"
for="foto">Pilih file</label>
</div>
</div>
</div>
</div>
</div>
<div class="ln_solid">
<div class="form-group">
<div class="col-md-6 offset-md-3">
<button type="submit" class="btn btn-
primary">Submit</button>
<button type="reset" class="btn btn-
success">Reset</button>
<a href="{{ route('produk.index') }}"><button
type="button" class="btn bn-success" style="color: black; padding: .85rem
1.40rem;">Back</button></a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection

2. READ

Kodingan View Read:

@extends('front/panel')

@section('content')

<!-- page content -->


<div class="right_col" role="main">
<div class="">
<div class="page-title">
<div class="title_left">
<h3>Detail <small>{{ $produk->nama }}</small></h3>
</div>
</div>

<div class="clearfix"></div>

<div class="row">
<div class="col-md-12 col-sm-12 ">
<div class="x_panel">
<div class="x_title">
<a href="{{ route('produk.index') }}" class="btn btn-
primary mb-3">Back</a>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="row">
<div class="col-sm-12">
<div class="card-box table-responsive">
<table id="datatable-responsive"
class="table table-striped table-bordered dt-responsive nowrap"
cellspacing="0" width="100%">
<tr>
<th>Nama:</th>
<td>{{ $produk->nama }}</td>
</tr>
<tr>
<th>Harga:</th>
<td>{{ $produk->harga }}</td>
</tr>
<tr>
<th>Stok:</th>
<td>{{ $produk->stok }}</td>
</tr>
<tr>
<th>Keterangan:</th>
<td>{{ $produk->keterangan }}</td>
</tr>
<tr>
<th>Foto:</th>
<td><img src="{{ asset('Barang/' .
$produk->foto) }}" alt="{{ $produk->nama }}" width="200"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /page content -->
@endsection

3. UPDATE

Kodingan View Update :

@extends('front.panel')

@section('content')
<div class="right_col" role="main">
<div class>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="x_panel">
<div class="x_title">
<h2>Form Edit <small>Produk</small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('produk.update', $produk->id)
}}" method="post" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Nama<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="text" name="nama" id="nama"
class="form-control" placeholder="Nama" value="{{ $produk->nama }}" required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Harga<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="text" name="harga" id="harga"
class="form-control" placeholder="Harga" value="{{ $produk->harga }}"
required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Stok<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="number" name="stok" id="stok"
class="form-control" placeholder="Stok" value="{{ $produk->stok }}" required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-
3 label-align">Keterangan<span class="required">*</span></label>
<div class="col-md-6 col-sm-6">
<input type="text" name="keterangan"
id="keterangan" class="form-control" placeholder="Keterangan" value="{{
$produk->keterangan }}" required>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-3
label-align">Foto<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 text-center">
<div class="custom-file">
<input type="file" name="foto"
id="foto" class="custom-file-input" value="{{ $produk->foto }}">
<label class="custom-file-label"
for="foto">Pilih file</label>
</div>
</div>
</div>
<div class="field item form-group">
<label class="col-form-label col-md-3 col-sm-3
label-align" for="Foto-preview">Foto Saat Ini:</label>
<br>
<div class="col-md-6 col-sm-6">
<img src="{{ asset('Barang/' . $produk-
>foto) }}">
</div>
</div>
<div class="ln_solid">
<div class="form-group">
<div class="col-md-6 offset-md-3">
<button type="submit" class="btn btn-
primary">Submit</button>
<button type="reset" class="btn btn-
success">Reset</button>
<a href="{{ route('produk.index')
}}"><button type="button" class="btn bn-success" style="color: black; padding:
.85rem 1.40rem;">Back</button></a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection

4. DELETE
Kodingan Controller Produk :

<?php

namespace App\Http\Controllers;

use App\Models\Produk;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\File;

class ProdukController extends Controller


{
public function index()
{
$title = "Hayikael | Table Produk";
$produks = Produk::all();
return view('table.produk.index', compact('produks', 'title'));
}

public function create()


{
$title = "Hayikael | Create Produk";
return view('table.produk.create', compact('title'));
}

public function store(Request $request)


{
$request->validate([
'nama' => 'required',
'harga' => 'required',
'stok' => 'required',
'keterangan' => 'required',
'foto' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
]);

$produk = new Produk([


'nama' => $request->get('nama'),
'harga' => $request->get('harga'),
'stok' => $request->get('stok'),
'keterangan' => $request->get('keterangan'),
]);

if ($request->hasFile('foto')) {
$foto = $request->file('foto');
$fotoName = time() . '.' . $foto->getClientOriginalExtension();
$image = Image::make($foto)->fit(300, 300);
$image->save(public_path('Barang/' . $fotoName));

$produk->foto = $fotoName;
}
$produk->save();
return redirect('produk')->with('success', 'Produk added
successfully');
}

public function show(Produk $produk)


{
$title = "Hayikael | Detail Produk";
return view('table.produk.detail', compact('produk', 'title'));
}

public function edit(Produk $produk)


{
$title = "Hayikael | Edit Produk";
return view('table.produk.edit', compact('produk', 'title'));
}

public function update(Request $request, Produk $produk)


{
$request->validate([
'nama' => 'required',
'harga' => 'required',
'stok' => 'required',
'keterangan' => 'required',
'foto' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
$produk->nama = $request->get('nama');
$produk->harga = $request->get('harga');
$produk->stok = $request->get('stok');
$produk->keterangan = $request->get('keterangan');

if ($request->hasFile('foto')) {
$foto = $request->file('foto');
$fotoName = time() . '.' . $foto->getClientOriginalExtension();

if ($produk->foto) {
$fotoPath = public_path('Barang/' . $produk->foto);
if (File::exists($fotoPath)) {
File::delete($fotoPath);
}
}

$image = Image::make($foto)->fit(300, 300);


$image->save(public_path('Barang/' . $fotoName));
$produk->foto = $fotoName;
}
$produk->save();
return redirect('produk')->with('success', 'Produk berhasil diubah');
}

public function destroy(Produk $produk)


{
if ($produk->foto) {
$fotoPath = public_path('Barang/' . $produk->foto);
if (File::exists($fotoPath)) {
File::delete($fotoPath);
}
}
$produk->delete();
return redirect('produk')->with('success', 'Produk berhasil dihapus');
}
}

Kodingan Model Produk

<?php

namespace App\Models;

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

class Produk extends Model


{
use HasFactory;
protected $table = 'produks';

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

You might also like