Last active
April 16, 2019 05:33
-
-
Save dwijonarko/ee7933b1acc7a7b4d895b61a2e9ed6ed to your computer and use it in GitHub Desktop.
CRUD Laravel - table categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Category; | |
class CategoryController extends Controller | |
{ | |
public function index(){ | |
$categories = Category::all(); | |
return view('category.index')->with('categories',$categories); | |
} | |
public function create(){ //untuk menampilkan form inputan | |
return view('category.create'); | |
} | |
public function store(Request $request){ | |
$category = new Category(); | |
$category->name = $request->name; | |
$category->save(); | |
return redirect('kategori'); | |
} | |
public function show($id){ | |
$category = Category::find($id); | |
return view('category.show')->with('category',$category); | |
} | |
public function edit($id){ | |
$category = Category::find($id); | |
return view('category.edit')->with('category',$category); | |
} | |
public function update($id,Request $request){ | |
$category = Category::find($id); | |
$category->name = $request->name; | |
$category->save(); | |
return redirect('kategori'); | |
} | |
public function destroy($id){ | |
$category = Category::find($id); | |
$category->delete(); | |
return redirect('kategori'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-8"> | |
<div class="card"> | |
<div class="card-header">New Kategori</div> | |
<div class="card-body"> | |
<form action="/kategori" method="post" class="form-horizontal"> | |
@csrf | |
<div class="form-group row"> | |
<label for="name" class="control-label col-md-4"> | |
Nama Kategori | |
</label> | |
<div class="col-md-8"> | |
<input type="text" name="name" id="name" class="form-control"> | |
</div> | |
</div> | |
<div class="row form-group"> | |
<div class="col-md-8 offset-md-4"> | |
<input type="submit" name="save" value="Simpan" class="btn btn-primary"> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-8"> | |
<div class="card"> | |
<div class="card-header">New Kategori</div> | |
<div class="card-body"> | |
<form action="/kategori/{{$category->id}}" method="POST" class="form-horizontal"> | |
@method('PUT') | |
@csrf | |
<div class="form-group row"> | |
<label for="name" class="control-label col-md-4"> | |
Nama Kategori | |
</label> | |
<div class="col-md-8"> | |
<input type="text" value="{{$category->name}}" name="name" id="name" class="form-control"> | |
</div> | |
</div> | |
<div class="row form-group"> | |
<div class="col-md-8 offset-md-4"> | |
<input type="submit" name="save" value="Simpan" class="btn btn-primary"> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-8"> | |
<div class="card"> | |
<div class="card-header"> | |
Kategori | |
<a href="/kategori/create" class="btn btn-sm btn-primary" style="float:right">Add Kategori</a> | |
</div> | |
<div class="card-body"> | |
<table class="table"> | |
<tr> | |
<th>id</th> | |
<th>Nama</th> | |
<th>Action</th> | |
</tr> | |
@foreach ($categories as $item) | |
<tr> | |
<td>{{ $item->id}}</td> | |
<td>{{ $item->name}}</td> | |
<td> | |
<a href="/kategori/{{$item->id}}" class="btn btn-sm btn-success">Detail</a> | |
<a href="/kategori/{{$item->id}}/edit" class="btn btn-sm btn-warning">Edit</a> | |
<form action="/kategori/{{$item->id}}" method="POST"> | |
@method('DELETE') | |
@csrf | |
<button class="btn btn-sm btn-danger"> | |
Delete | |
</button> | |
</form> | |
</td> | |
</tr> | |
@endforeach | |
</table> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-8"> | |
<div class="card"> | |
<div class="card-header">Kategori {{$category->name}}</div> | |
<div class="card-body"> | |
<div class="row"> | |
<div class="col-md-4">Nama</div> | |
<div class="col-md-8">{{$category->name}}</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-4">Created At</div> | |
<div class="col-md-8">{{$category->created_at}}</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-4">Updated At</div> | |
<div class="col-md-8">{{$category->updated_at}}</div> | |
</div> | |
<div class="row col-md-12"> | |
<a href="/kategori" class="btn btn-sm btn-success">Kembali</a> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*...isi kode sebelumnya ... */ | |
//localhost:8000/kategori | |
Route::get('/kategori','CategoryController@index'); | |
Route::get('/kategori/create','CategoryController@create'); | |
Route::post('/kategori','CategoryController@store'); | |
Route::get('/kategori/{id}','CategoryController@show'); | |
Route::get('/kategori/{id}/edit','CategoryController@edit'); | |
Route::put('/kategori/{id}','CategoryController@update'); | |
Route::delete('/kategori/{id}','CategoryController@destroy'); | |
/*...isi kode selanjutnya ... */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateCategoriesTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('categories', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->string('name'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('categories'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment