Make a car rental admin using laravel 9 part 3: Management Car

php artisan make:migration add_image_to_cars_table --table=cars
Schema::table('cars', function (Blueprint $table) {
$table->string('image');
});
php artisan make:controller CarController --resource
'links' => [
public_path('storage') => storage_path('app'),
],
php artisan storage:link
Route::middleware(['auth'])->resource('/car', CarController::class);
<div class="sb-sidenav-menu-heading">Data</div>
<a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#collapseLayouts" aria-expanded="false" aria-controls="collapseLayouts">
<div class="sb-nav-link-icon"><i class="fas fa-columns"></i></div>
Car
<div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
</a>
<div class="collapse" id="collapseLayouts" aria-labelledby="headingOne" data-bs-parent="#sidenavAccordion">
<nav class="sb-sidenav-menu-nested nav">
<a class="nav-link" href="{{ route('car.index') }}">All Car</a>
</nav>
</div>

Index

public function index(){
$cars = Car::latest()->paginate(10);
return view('cars.index');
}
use Illuminate\Pagination\Paginator;public function boot()
{
Paginator::useBootstrapFive();
}

Create

public function create()
{
return view('cars.create');
}
public function store(Request $request){
$data = $request->validate([
'name' => 'required',
'plat' => 'required',
'description' => 'required',
'price' => 'required|integer',
'status' => 'required',
]);
if($request->file('image')){
$data['image'] = $request->file('image')->store('cars');
}
Car::create($data);
return redirect()->route('car.index')->with('success', 'Data mobil berhasil ditambahkan');
}

Delete

<td>
<form class="d-inline" action="{{ route('car.destroy', $car->id) }}" method="POST">
@csrf
@method('delete')
<button class="btn btn-danger" onclick="return confirm('Hapus data mobil ini?')">Hapus</button>
</form>
<a href="{{ route('car.edit', $car->id) }}" class="btn btn-success">Edit</a>
<a href="#" class="btn btn-info">Detail</a>
</td>
public function destroy(Car $car){
if($car->image){
Storage::delete($car->image);
}
Car::destroy($car->id);
return redirect()->route('car.index')->with('success', 'Data mobil berhasil dihapus');
}

Show

<button type="button" class="btn btn-info" data-bs-toggle="modal" data-bs-target="#detail{{ $car->id }}">Detail</button>
<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="detail{{ $car->id }}" tabindex="-1" aria-labelledby="detail{{ $car->id }}Label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Nama : {{ $car->name }}</p>
<p>Plat : {{ $car->plat }}</p>
<p>Deskripsi : {{ $car->description }}</p>
<p>Harga : {{ number_format($car->price, 2) }}</p>
<p>Status : @if($car->status == 1)
<span class="badge bg-success">Available</span>
@else
<span class="badge bg-danger">Not Available</span>
@endif</p>
<p>
<img src="{{ asset('storage/' . $car->image) }}" width="400" alt="">
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Update

public function edit(Car $car){
return view('cars.edit', ['car'=> $car]);
}public function update(Request $request, Car $car){
$data = $request->validate([
'name' => 'required',
'plat' => 'required',
'description' => 'required',
'price' => 'required|integer',
'status' => 'required',
]);
if($request->file('image')){
if($request->oldImage){
Storage::delete($request->oldImage);
}
$data['image'] = $request->file('image')->store('cars');
}
$car->update($data);
return redirect()->route('car.index')->with('success', 'Data mobil berhasil diedit');
}

Search

<div class="mt-3 justify-content-center">
<form action="{{ route('car.index') }}" method="GET">
<div class="row">
<div class="col">
<div class="input-group mb-3">
<input type="text" value="{{ Request::input('search') }}" class="form-control" placeholder="search . . ." name="search">
</div>
</div>
<div class="col-1">
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</div>
</form>
</div>
$search = $request->search;
$cars = Car::where('name', 'like', '%' . $search .'%')
->orWhere('plat', 'like', '%' . $search .'%')
->orWhere('price', 'like', '%' . $search .'%')
->latest()
->paginate(10);
return view('cars.index', compact('cars'));

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
M Alfa Riza

M Alfa Riza

FullStack Developer | Vue js | React js | Laravel