Pagi gaes
kali ini kita mau buat crud dari laravel 5.1 , sebelum baca article ini
pastikan anda sudah membaca cara membuat project di laravel 5.1 klo belum liat
aja di sini.
Ok
langsung aje, pertama buka cmd kamu dan masuk ke dalam project laravel yang
sudah di buat klo saya klik kanan mouse di project trus pilih Use Composer
here
1.
Testing laravel
untuk
test laravel coba masuk dalam project dan ketikan di cmd :
php
artisan serve
maka akan
keluar Laravel development started on http://localhost:8000, sekarang
masukan alamat itu ke browser kita.
jika
berhasil maka akan keluar
2.
Siapkan database kita
yang
saya buat database bername books, untuk table jangan lupa tambahkan created_at
dan updated_at bertipe Timestamp. karna ini biasanya ada dlam setiap project
laravel.
3. Setting
database
APP_DEBUG=true
APP_KEY=tqI5Gj43QwYFzSRhHc7JLi57xhixnDYH
DB_HOST=localhost
DB_DATABASE=library
DB_USERNAME=root
DB_PASSWORD=your_mysql_password
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
4. Buat controller pada book di project bookstore
untuk membuat controller di laravel cukup mudah anda hanya cukup buka cmd lalu ketikan :php artisan make:controller BooksController (nama_controller)
maka akan tergenerate sendiri controller nya dan kalian bisa cek di dalam folder
app/Http/Controller
dan hasilnya akan seperti ini :
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
5. Buat Model
sama seperti controller membuat model pun hanya cukup ketikan pada cmd :
php artisan make:model Book
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Book extends Model { // }
note : fungsi make dilakukan untuk membuat class atau fungsi lainnya.
6. Install Form dan Html
seperti biasa buka cmd lalu ketikan : composer require illuminate/html
ini untuk menambah library pada project.
setelah berhasil install buka file config/app.php
setelah berhasil install buka file config/app.php
lalu tambahkan pada providers :
Illuminate\Html\HtmlServiceProvider::class
dan juga tambahkan pada aliases :
'Form' => 'Illuminate\Html\FormFacade',
'Html' => 'Illuminate\Html\HtmlFacade',
7. Setting controller
buka file routes.php
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ /* Route::get('/', 'WelcomeController@index'); Route::get('home', 'HomeController@index'); Route::controllers([ 'auth' => 'Auth\AuthController', 'password' => 'Auth\PasswordController', ]);*/ Route::resource('books','BookController');
note : pada route ada books dan BookController,
BookController yang kita sudah ketahui itu adalah controllernya sedangkan books adalah inisialisasi saja atau bisa dikatakan bebas apa saja.
untuk mengetahui dalam controller ada apa saja yang bisa di akses coba ketikan pada cmd
php artisan route:list
Domain | Method | URI | Name | Action | Midleware |
---|---|---|---|---|---|
GET|HEAD | books | books.index | App\Http\Controllers\BookController@index | ||
GET|HEAD | books/create | books.create | App\Http\Controllers\BookController@create | ||
POST | books | books.store | App\Http\Controllers\BookController@store | ||
GET|HEAD | books/{books} | books.show | App\Http\Controllers\BookController@show | ||
GET|HEAD | books/{books}/edit | books.edit | App\Http\Controllers\BookController@edit | ||
PUT | books/{books} | books.update | App\Http\Controllers\BookController@update | ||
PATCH | books/{books} | App\Http\Controllers\BookController@update | |||
DELETE | books/{books} | books.destroy | App\Http\Controllers\BookController@destroy |
note : URI adalah url yg kita lihat dalam browser dan Name adalah fungsi yang kita deklarasikan tadi jika kita perhatikan isi name adalah books inisialisasi dari controller dan .index dll adalah function yang ada dalam controller tsb.
8. Buat data dummy
8. Buat data dummy
9. Buat template header
buka foler bookstore/resources/view
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>BookStore</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap /3.3.4/css/bootstrap.min.css"> </head> <body> <div class="container"> @yield('content') </div> </body> </html>
10. Buat method list dan layout view
buka file app/Http/Controllers/BookController.php
buka file app/Http/Controllers/BookController.php
<?php namespace App\Http\Controllers; use App\Book; //ini cara memanggil model use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class BookController extends Controller { /** * Display a listing of the resource. * * @return Response */ public function index() { // $books=Book::all(); return view('books.index',compact('books')); }
kemudian buka file bookstore/resources/view
tambahkan file index.blade.php dan isi dengan
@extends('layout/template') @section('content') <h1>Peru BookStore</h1> <a href="{{url('/books/create')}}" class="btn btn-success">Create Book</a> <hr> <table class="table table-striped table-bordered table-hover"> <thead> <tr class="bg-info"> <th>Id</th> <th>ISBN</th> <th>Title</th> <th>Author</th> <th>Publisher</th> <th>Thumbs</th> <th colspan="3">Actions</th> </tr> </thead> <tbody> @foreach ($books as $book) <tr> <td>{{ $book->id }}</td> <td>{{ $book->isbn }}</td> <td>{{ $book->title }}</td> <td>{{ $book->author }}</td> <td>{{ $book->publisher }}</td> <td><img src="{{asset('img/'.$book->image.'.jpg')}}" height="35" width="30"></td> <td><a href="{{url('books',$book->id)}}" class="btn btn-primary">Read</a></td> <td><a href="{{route('books.edit',$book->id)}}" class="btn btn-warning">Update</a></td> <td> {!! Form::open(['method' => 'DELETE', 'route'=>['books.destroy', $book->id]]) !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} </td> </tr> @endforeach </tbody> </table> @endsection
sekarang buka pada browser anda localhost:8000/books
11. Membuat method dan layout untuk Read
buka file bookstore/resources/view/books
tambah kan file show.blade.php
@extends('layout/template') @section('content') <h1>Book Show</h1> <form class="form-horizontal"> <div class="form-group"> <label for="image" class="col-sm-2 control-label">Cover</label> <div class="col-sm-10"> <img src="{{asset('img/'.$book->image.'.jpg')}}" height="180" width="150" class="img-rounded"> </div> </div> <div class="form-group"> <label for="isbn" class="col-sm-2 control-label">ISBN</label> <div class="col-sm-10"> <input type="text" class="form-control" id="isbn" placeholder={{$book->isbn}} readonly> </div> </div> <div class="form-group"> <label for="title" class="col-sm-2 control-label">Title</label> <div class="col-sm-10"> <input type="text" class="form-control" id="title" placeholder={{$book->title}} readonly> </div> </div> <div class="form-group"> <label for="author" class="col-sm-2 control-label">Author</label> <div class="col-sm-10"> <input type="text" class="form-control" id="author" placeholder={{$book->author}} readonly> </div> </div> <div class="form-group"> <label for="publisher" class="col-sm-2 control-label">Publisher</label> <div class="col-sm-10"> <input type="text" class="form-control" id="publisher" placeholder={{$book->publisher}} readonly> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <a href="{{ url('books')}}" class="btn btn-primary">Back</a> </div> </div> </form> @stop
dan pada BookController tambahkan
public function show($id) { $book=Book::find($id); return view('books.show',compact('book')); }
12. Untuk Create
buka file bookstore/resources/view/books
tambahkan file create.blade.php
@extends('layout.template') @section('content') <h1>Create Book</h1> {!! Form::open(['url' => 'books']) !!} <div class="form-group"> {!! Form::label('ISBN', 'ISBN:') !!} {!! Form::text('isbn',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Title', 'Title:') !!} {!! Form::text('title',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Author', 'Author:') !!} {!! Form::text('author',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Publisher', 'Publisher:') !!} {!! Form::text('publisher',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Image', 'Image:') !!} {!! Form::text('image',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::submit('Save', ['class' => 'btn btn-primary form-control']) !!} </div> {!! Form::close() !!} @stop
dan pada BookController tambahkan
public function create() { return view('books.create');// ini halaman view } /** * Store a newly created resource in storage. * * @return Response */ public function store() { $book=Request::all(); Book::create($book);//ini insert ke database return redirect('books'); }
jangan lupa tambahkan pada model
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Book extends Model { // protected $fillable=[ 'isbn', 'title', 'author', 'publisher', 'image' ]; }
13. Edit Books
buka file bookstore/resources/view/books
tambahkan edit.blade.php
@extends('layout.template') @section('content') <h1>Update Book</h1> {!! Form::model($book,['method' => 'PATCH','route'=>['books.update',$book->id]]) !!} <div class="form-group"> {!! Form::label('ISBN', 'ISBN:') !!} {!! Form::text('isbn',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Title', 'Title:') !!} {!! Form::text('title',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Author', 'Author:') !!} {!! Form::text('author',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Publisher', 'Publisher:') !!} {!! Form::text('publisher',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('Image', 'Image:') !!} {!! Form::text('image',null,['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::submit('Update', ['class' => 'btn btn-primary']) !!} </div> {!! Form::close() !!} @stop
Pada controller tambahkan
public function edit($id) { $book=Book::find($id); return view('books.edit',compact('book')); } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { // $bookUpdate=Request::all(); $book=Book::find($id); $book->update($bookUpdate); return redirect('books'); }
14. Delete Book
tambahkan code pade app/Http/Controllers/BookController.php
public function destroy($id) { Book::find($id)->delete(); return redirect('books'); }
jeng jeng... selesai :D
klo masih kurang jelas bisa liat sumber nya di http://learninglaravel.net/
6 comments
Write commentslokasi routes.php dimana ?
Replylokasi routes.php dimana ? routes.php tidak ditemukan setelah mencoba langkah-langkahnya
Replyudah nemu gan ?
Replymakasih gan
Replydi laravel 5.3 keatas udh ga ad mank routes.php.. tp diganti di folder route/web.php
Replyvisit this link
Replyhttps://jatimtimes.com
https://malangtimes.com
https://jatimtimes.com/baca/238055/20210322/085900/buruan-miliki-unitnya-apartemen-the-kalindra-segera-groundbreaking
Emoticon Emoticon