Last active
May 10, 2019 01:33
-
-
Save dwijonarko/386e3fd10f5fc8aa0d6db07283982ce0 to your computer and use it in GitHub Desktop.
CRUD Laravel - Articles
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 Article</div> | |
<div class="card-body"> | |
<form action="/artikel" method="post" class="form-horizontal" enctype="multipart/form-data"> | |
@csrf | |
<div class="form-group row"> | |
<label for="title" class="control-label col-md-4"> | |
Judul | |
</label> | |
<div class="col-md-8"> | |
<input type="text" name="title" id="title" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="content" class="control-label col-md-4"> | |
Isi Artikel | |
</label> | |
<div class="col-md-8"> | |
<textarea name="content" id="content" cols="30" rows="10" class="form-control"></textarea> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="category_id" class="control-label col-md-4"> | |
Kategori | |
</label> | |
<div class="col-md-8"> | |
<select name="category_id" id="category_id" class="form-control"> | |
@foreach ($categories as $name=>$id) | |
<option value="{{$id}}">{{$name}}</option> | |
@endforeach | |
</select> | |
</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">Edit Article</div> | |
<div class="card-body"> | |
<form action="/artikel/{{$article->id}}" method="post" class="form-horizontal"> | |
@method('PUT') | |
@csrf | |
<div class="form-group row"> | |
<label for="title" class="control-label col-md-4"> | |
Judul | |
</label> | |
<div class="col-md-8"> | |
<input type="text" name="title" id="title" class="form-control" value="{{$article->title}}"> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="content" class="control-label col-md-4"> | |
Isi Artikel | |
</label> | |
<div class="col-md-8"> | |
<textarea name="content" id="content" cols="30" rows="10" class="form-control">{{$article->content}}</textarea> | |
</div> | |
</div> | |
<div class="form-group row"> | |
<label for="category_id" class="control-label col-md-4"> | |
Kategori | |
</label> | |
<div class="col-md-8"> | |
<select name="category_id" id="category_id" class="form-control"> | |
@foreach ($categories as $name=>$id) | |
<option value="{{$id}}" {{ ($id==$article->category_id)?"selected":"" }} >{{$name}}</option> | |
@endforeach | |
</select> | |
</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"> | |
Artikel | |
<a href="/artikel/create" class="btn btn-sm btn-primary" style="float:right">Add Artikel</a> | |
</div> | |
<div class="card-body"> | |
<table class="table"> | |
<tr> | |
<th>id</th> | |
<th>Judul</th> | |
<th>Konten</th> | |
<th>Kategori</th> | |
<th>Penulis</th> | |
<th>Action</th> | |
</tr> | |
@foreach ($articles as $item) | |
<tr> | |
<td>{{ $item->id}}</td> | |
<td>{{ $item->title}}</td> | |
<td>{{ $item->content}}</td> | |
<td>{{ $item->category_id}}</td> | |
<td>{{ $item->user_id}}</td> | |
<td> | |
<a href="/artikel/{{$item->id}}" class="btn btn-sm btn-success">Detail</a> | |
<a href="/artikel/{{$item->id}}/edit" class="btn btn-sm btn-warning">Edit</a> | |
<form action="/artikel/{{$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
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Article extends Model | |
{ | |
protected $fillable = ['title','content','category_id','user_id']; | |
} |
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">Artikel {{$article->title}}</div> | |
<div class="card-body"> | |
<div class="row"> | |
<div class="col-md-4">Judul</div> | |
<div class="col-md-8">{{$article->title}}</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-4">Content</div> | |
<div class="col-md-8">{{$article->content}}</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-4">Kategori</div> | |
<div class="col-md-8">{{$article->category_id}}</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-4">User</div> | |
<div class="col-md-8">{{$article->user_id}}</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-4">Created At</div> | |
<div class="col-md-8">{{$article->created_at}}</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-4">Updated At</div> | |
<div class="col-md-8">{{$article->updated_at}}</div> | |
</div> | |
<div class="row col-md-12"> | |
<a href="/artikel" 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 | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Article; | |
use App\Category; | |
class ArticleController extends Controller | |
{ | |
public function index(){ | |
$articles = Article::all(); //mengambil data seluruh artikel | |
return view('article.index')->with(compact('articles')); | |
} | |
public function create(){ //untuk menampilkan form inputan | |
$categories = Category::pluck('id','name'); | |
return view('article.create')->with(compact('categories')); | |
// return $categories; | |
} | |
public function store(Request $request){ | |
$input = $request->all(); | |
$input['user_id']=1; | |
Article::create($input); | |
return redirect('artikel'); | |
} | |
public function show($id){ | |
$article = Article::find($id); | |
return view('article.show')->with(compact('article')); | |
} | |
public function edit($id){ | |
$categories = Category::pluck('id','name'); | |
$article = Article::findOrFail($id); | |
return view('article.edit')->with(compact('article'))->with(compact('categories')); | |
} | |
public function update($id,Request $request){ | |
$article = Article::find($id); | |
$article->title = $request->title; | |
$article->content = $request->content; | |
$article->category_id = $request->category_id; | |
$article->save(); | |
return redirect('artikel'); | |
} | |
public function destroy($id){ | |
$article = Article::find($id); | |
$article->delete(); | |
return redirect('artikel'); | |
} | |
} |
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 | |
/* kode sebelumnya ... /** | |
//localhost:8000/artikel | |
Route::get('/artikel','ArticleController@index'); | |
Route::get('/artikel/create','ArticleController@create'); | |
Route::post('/artikel','ArticleController@store'); | |
Route::get('/artikel/{id}','ArticleController@show'); | |
Route::get('/artikel/{id}/edit','ArticleController@edit'); | |
Route::put('/artikel/{id}','ArticleController@update'); | |
Route::delete('/artikel/{id}','ArticleController@destroy'); |
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 CreateArticlesTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('articles', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->string('title'); | |
$table->text('content'); | |
$table->bigInteger('category_id')->unsigned(); | |
$table->foreign('category_id')->references('id')->on('categories'); | |
$table->bigInteger('user_id')->unsigned(); | |
$table->foreign('user_id')->references('id')->on('users'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('articles'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment