MHSyahfarel PWF CRUDLaravel
MHSyahfarel PWF CRUDLaravel
MHSyahfarel PWF CRUDLaravel
Disusun oleh :
(210402114)
2023
1. 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
@extends('front/panel')
@section('content')
<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
@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;
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');
}
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);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
protected $fillable = [
'nama',
'harga',
'stok',
'keterangan',
'foto',
];
}