Professional Documents
Culture Documents
sebagai berikut:
„Hello World‟, migration dan seeding, Kali ini, kita akan membahas
Twitter Bootstrap
akan menemukan tiga folder didalamnya yaitu folder css, fonts dan
Laravel.
Bootstrap di dalam direktori publik laravel
akan kita buat. Oleh karena itu, marilah kita lupakan sejenak
Database Tabel
menampung data yang akan kita gunakan dalam tutorial kali ini.
-----------------------------------------------------//
Schema::create('books', function($table){
$table->increments('id');
$table->string('title')->unique();
$table->string('author');
$table->string('description');
$table->string('price');
$table->timestamps();
});
Schema::drop('books');
}
}
?>
line. Maka secara otomatis akan tercipta sebuah tabel baru bernama
CRUD
Untuk itu mari kita bahas satu persatu mulai dari Create
Create
<!DOCTYPE html>
<html lang='en'>
<head>
<title>CRUD App</title>
<meta name='viewport' content='width=device-width, initial-
{{ HTML::style('css/bootstrap.min.css') }}
</head>;
<body>
@yield('content')
{{ HTML::script('js/jquery.min.js') }}
{{ HTML::script('js/bootstrap.js') }}
</body>
</html>
bernama newbook.blade.php:
@extends('layout')
@section('content')
@stop
form yang akan kita buat pada browser. Pada file tersebut anda
tidak perlu menulis lagi tag `html`, `title` , `body` karena sudah di-
@extends('layout')
@section('content')
<div class='container'>
<h3>Insert new book</h3>
{{ Form::open()}}
{{Form::label('title', 'Title') }}
{{Form::label('author', 'Author') }}
{{Form::label('description', 'Description') }}
{{Form::label('price', 'Price') }}
{{ Form::close() }}
</div>
@stop
Route::get('newbook', function()
return View::make('newbook');
});
Akses route tersebut melalui browser, maka akan tampil form seperti
berikut ini:
Memasukan data ke database
nama tabel yang digunakan, karena pada contoh kali ini kita
Ok, setelah Model anda buat, selanjutnya mari kita buat controller.
nama BookController.php :
<?php
?>
dalam database:
<?php
$book->title = Input::get('title');;
$book->author = Input::get('author');;
$book->description = Input::get('description');;
$book->price = Input::get('price');;
$book->save();
?>
Bagian Terakhir
Konklusi
Membuat route.
Membuat looping.
berikut:
Route::get('booklist', function(){
});
Setelah itu, kita akan mengambil semua data dari tabel books yang
telah kita buat sebelumnya. Ingat, untuk mengakses tabel books kita
Route::get('booklist', function(){
$books = Book::all();
});
Route::get('booklist', function(){
$books = Book::all();
});
error akan muncul. Itu karena view bernama listbook masih belum
kita buat. Oleh karena itu, mari kita buat view tersebut. Buat
@section('content')
@stop
semua data dari tabel books yang bisa kita tampilkan dengan
bantuan looping :
@extends('layout')
@section('content')
<section class="container">
<table class="table">
<tr>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>Price</th>
</tr>
@foreach($booksdata as $book)
<tr>
</tr>
@endforeach
</table>
</section>
@stop
http://localhost/booklist
Bersambung
@extends('layout')
@section('content')
<section class="container">
<table class="table">
<tr>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>Price</th>
<th>#</th>
</tr>
@foreach($booksdata as $book)
<tr>
>id))}}</td>
</tr>
@endforeach
</table>
</section>
@stop
Pada kode diatas, kita menambahkan link_to_action yang
ErrorException
/var/www/laravel/app/views/listbook.blade.php)
Route::get('book/edit/{id}', 'BookController@edit');
# code...
Jika, anda klik pada salah satu link yang ditampilkan pada
berikut:
$book = Book::find($id);
Jika, anda jalankan lagi maka kali ini bukan halaman kosong
berikut:
InvalidArgumentException
@extends('layout')
@section('content')
<div class='container'>
<h3>Edit book</h3>
{{ Form::open() }}
{{Form::hidden('id', $book->id)}}
{{Form::label('title', 'Title') }}
{{Form::label('author', 'Author') }}
control'))}}
{{Form::label('description', 'Description') }}
'form-control'))}}
{{Form::label('price', 'Price') }}
</div>
@stop
Update
pada BookController:
$id = Input::get('id');
$book = Book::find($id);
$book->title = Input::get('title');
$book->author = Input::get('author');
$book->description = Input::get('description');
$book->price = Input::get('price');
$book->save();
}
Route::post('book/update', 'BookController@update');
{{ Form::open()) }}
menjadi
Sekarang, cobalah anda ubah judul dari buku tersebut, lalu klik
tombol update. Maka, data judul buku tersebut akan berubah pula
di database.
@extends('layout')
@section('content')
<section class="container">
<table class="table">
<tr>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>Price</th>
<th>#</th>
</tr>
@foreach($booksdata as $book)
<tr>
array($book->id))}}</td>
</tr>
@endforeach
</table>
</section>
@stop
ErrorException
/var/www/laravel/app/views/listbook.blade.php)
Route::get('book/delete/{id}', 'BookController@delete');
Namun, apabila link tersebut anda klik, tetap akan terjadi error,
berikut:
public function delete($id)
# code...
Execute Delete
Sekarang, jika anda klik „delete‟ maka tidak akan muncul lagi
berikut:
$book = Book::find($id);
$book->delete();
return Redirect::to('booklist');
Sekarang, jika anda klik link delete maka data buku yang
Saya rasa anda sudah paham tentang apa itu autentikasi, jadi saya
sebagai berikut:
method „up‟:
Schema::create('users', function($table)
{
$table->increments('id');Â
$table->string('firstname', 20);Â
$table->string('lastname', 20);Â
$table->string('email', 100)->unique();Â
$table->string('password', 64);Â
$table->timestamps();Â
});
berikut:
Schema::drop('users');
command line:
php artisan controller:make UserController
Pada fungsi register diatas, kita akan diarahkan pada sebuah view
bernama „register.blade.php‟:
@extends('layout')
@section('content')
@if(Session::has('pesan'))
@endif
{{Form::label('email', 'Email') }}
{{Form::label('password', 'Password') }}
<br>
primary')) }}
Setelah itu, buatlah route baru pada file routes.php seperti berikut
ini:
Route::get('register', 'UserController@register');
Route::post('store', 'UserController@store');
http:/localhost/laravel/register
Registrasikan Pengguna
dari form yang barusan kita buat ke dalam database. Pada form
Itu berarti, ketika disubmit, data dari form tersebut akan di-handle
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
Selain itu, buatlah dua buah route baru pada file routes.php
sebagai berikut:
Route::get('login', 'UserController@login');
Route::post('authenticate', 'UserController@authenticate');
Halaman Login
return View::make('login');
@extends('layout')
@section('content')
<div class="container">
<h2>Login</h2>
<hr />
@if(Session::has('pesan_error'))
@endif
{{Form::label('email', 'Email') }}
{{Form::label('password', 'Password') }}
{{Form::password('password', array('class' => 'form-control'))}}
{{Form::close() }}
</div>
</div>
@stop
Autentikasi
Input::get('password'))))
else{
}
Sekarang, jika anda tekan tombol „login‟ dan autentikasi berhasil
melihat tulisan „login berhasil‟. Nah, kali ini kita akan membuat
„DashboardController‟:
<?php
return View::make('dashboard');
}
}
?>
berikut:
Route::get('dashboard', 'DashboardController@index');
function authenticate(){
Input::get('password'))))
return Redirect::to('dashboard');
Sekarang, saatnya kita membuat fungsi logout. Untuk itu, kita akan
@extends('layout')
@section('content')
role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling --
>
collapse-1">
logout</a></li>
</ul>
</nav>
<div class="container">
<h1>Dashboard</h1>
</div>
@stop
Selanjutnya, perhatikan pada halaman dashboard diatas terdapat
Pada atribut href, kita bisa melihat bahwa kita akan diarahkan
pada route „logout‟ apabila link tersebut kita klik. Namun, route
„logout‟ masih belum kita buat, oleh itu tambahkan route „logout‟
Route::get('logout', 'UserController@logout');
berikut:
Auth::logout();
@if(Session::has('pesan'))
{{ Session::get('pesan') }}
@endif
Kesimpulan
halaman login. Dan masih ada beberapa hal lagi yang harus
Generators
"require-dev": {
"way/generators": "2.*"
},
bisa melihat perintah apa saja yang bisa anda gunakan pada
command-line.
Membuat Controller
dan sebagainya.
Membuat Migration
Sebenarnya, untuk membuat migration anda juga bisa
menggunakan perintah:
Membuat Model
<?php
class Example extends \Eloquent {
Membuat View
command-line:
Semoga Bermanfaat.
object pada php, maka anda tidak akan terlalu kesulitan bekerja
cara demikian, kita tidak perlu membuat model. Hal ini sah-sah
lebih rapi dan mudah dibaca dan juga tentu saja lebih cepat serta
Kali ini kita akan membuat dua buah Model bernama „Penulis‟
berikut:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
/**
* Run the migrations.
* @return void
*/
$table->increments('id');
$table->string('judul');
$table->text('body');
$table->integer('author_id');
$table->timestamps();
});
/**
* @return void
*/
Schema::drop('articles');
}
?>
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
/**
* @return void
*/
$table->increments('id');
$table->string('username')->unique();
$table->text('bio');
$table->timestamps();
});
/**
* @return void
*/
Schema::drop('authors');
?>
berikut:
Create
// app/routes.php
Route::get('/', function()
$author->save();
});
Select
// app/routes.php
Route::get('/', function()
{
$authors = Author::all()->first();
echo $authors->username;
echo $authors->bio;
});
pertama dari data yang ada pada query select. Untuk mengambil
semua data, cukup hilangkan method first tersebut, setelah itu data
// app/routes.php
Route::get('/', function()
$authors = Author::all();
foreach($authors as $author)
echo $author->username;
echo $author->bio;
});
Update
// app/routes.php
Route::get('/', function()
$author->save();
});
Perhatikan bahwa contoh diatas hampir sama dengan contoh
Delete
// app/routes.php
Route::get('/', function()
$author->delete();
});
Route::get('/', function()
$author->save();
$author->save();
$author->save();
});
Route::get('/', function()
$author = Author::find(1);
kami
$article->author_id = $author->id;
$article->save();
kami
$article2->author_id = $author->id;
$article2->save();
});
Menggunakan hasMany
Seorang penulis (author) biasanya memiliki banyak artikel. Pada
<?php
return $this->hasMany('Article');
?>
akan menjadi „this author has many article / author ini memiliki
Menggunakan belongsTo
<?php
return $this->belongsTo('Author');
}
} ?>
berikut:
Route::get('/', function()
foreach($author->articles as $article){
echo $article->title;
echo $article->body;
}
});
harapan‟.
Route::get('/', function()
>first();
echo $article->author->username;
echo $article->author->bio;
});
Perhatikan bahwa model yang digunakan pada contoh diatas
Route::get('/', function()
$articles = Article::all();
foreach($articles as $article){
echo $article->author->username;
echo $article->title;
});
$paintings = Painting::with('author')->get();
foreach($articles as $article){
echo $article->author->username;
echo $article->title;
});
Dengan cara tersebut kueri yang digunakan bisa lebih sedikit yaitu
hanya menjadi 2 query saja. Anda bisa melihat jumlah query yang
Semoga bermanfaat.
"require": {
"laravel/framework": "4.1.25",
"gloudemans/shoppingcart": "dev-master"
},
'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'
internet.
Schema::create('products', function($table){
$table->increments('id')->unsigned();
$table->string('name');
$table->string('description', 500);
$table->string('price', 10);
$table->timestamps();
});
Tabel produk yang saya buat diatas saya buat sesederhana mungkin
Bagi anda yang belum tahu cara migrasi tabel dengan laravel, bisa
baca disini. Anda juga bisa membuat tabel secara manual dengan
<?php
?>
seperti berikut:
Route::get('/', function()
$products = Product::all();
});
Lalu pada folder app/views buat sebuah view baru dengan nama
<div class="container">
@foreach($products as $product)
<div class="col-md-3">
<div class="panel">
<div class="panel-heading">
{{ $product->name }}
</div>
<div class="panel-body">
{{ $product->description }}
</div>
<div class="panel-footer">
</div>
</div>
</div>
@endforeach
</div>
NOTE: Saya menggunakan Twitter Bootstrap sebagai CSS Framework,
Yap, akhirnya kita sampai juga pada bahasan utama kita. Jika
app/routes.php:
Route::get('product/cart/{id}', function($id){
});
Route::get('product/cart/{id}', function($id){
$product = Product::find($id);
$id = $product->id;
$name = $product->name;
$qty = 1;
$price = $product->price;
'large'));
Cart::add($data);
$cart_content = Cart::content(1);
return View::make('productcart')->with('cart_content',
$cart_content);
});
setelah itu, kita buat sebuah view baru productcart.blade.php.
<div class="container">
<hr>
{{ Message::success() }}
<div class="panel">
<div class="panel-heading">Cart</div>
<thead>
<tr>
<th width="8">No</th>
<th>Price</th>
<th width="100">Quantity</th>
<th width="200">Action</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
@foreach($cart_content as $cart)
<tr>
<td>{{ $i }}</td>
<td>
>rowid) }}">delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="panel-footer">
white">Continue Shopping</a>
info">Checkout</a>
</div>
</div>
</div>
Menghapus item dari dalam shopping cart
Jika anda perhatikan view yang telah anda buat diatas, akan
itu, mari kita buat route tersebut. Tambahkan syntax berikut pada
app/routes.php:
Route::get('cart/delete/{id}' , function(){
Cart::remove($id);
return View::make('productcart')->with('cart_content',
$cart_content);
});
Melakukan checkout
sebagai berikut:
Route::get('cart/checkout' , function(){
$formid = str_random();
$cart_content = Cart::content(1);
foreach ($cart_content as $cart) {
$product = Product::find($cart->id);
$transaction->product_id = $cart->id;
$transaction->form_id = $formid;
$transaction->qty = $cart->qty;
>qty;
$transaction->status = 'unpaid';
$transaction->save();
Cart::destroy();
});
Perhatikan kode diatas, pada saat melakukan checkout, kita akan
Schema::create('transactions', function($table){
$table->increments('id')->unsigned();
$table->string('product_id');
$table->string('form_id');
$table->string('qty');
$table->string('total_price');
$table->string('status');
$table->timestamps();
});
<?php
?>
Sekarang, jika anda kembali mencoba melakukan checkout. Maka
transaction.
semoga bermanfaat.
yang kita buat, kita perlu melakukan image resizing, image crop,
"require": {
"laravel/framework": "4.1.25",
"intervention/image": "1.*",
},
Setelah itu jalankan perintah composer update melalui command-
ini.
'Intervention\Image\ImageServiceProvider',
intervention/image.
$file_path = 'uploads/image.png';
$img = Image::make($file_path);
$img->crop(500, 300);
$img->save($file_path);
Image::make($file_path);.
gambar asli tersebut yang kita crop. Jika, anda ingin gambar yang
$file_path = 'uploads/image.png';
$img = Image::make($file_path);
$img->resize(500, 300);
$img->save($file_path);
Proses resize sangat mirip dengan crop, kita hanya mengganti $img-
pada contoh diatas ukuran yang kita gunakan adalah 500 x 300.
akan hilang.
500 x 300 menjadi 500 x 50, tentu gambar akan menjadi terlihat
menjadi 500 x 250 (1000 x 500 = 2:1, 500 x 250 = 2:1 —- sama-
sama 2:1).
Menambahkan Watermark
mudah:
$file_path = 'uploads/image.png';
$watermark = 'uploads/watermark.png';
$img = Image::make($file_path);
$img->insert($watermark);
$img->save($file_path);
http://image.intervention.io/
Semoga bermanfaat.
menggunakan pagination.
berikut:
$users = User::paginate(60);
$users = User::paginate(50);
{{ $users->links(); }}
Contoh kode lengkap yang bisa anda gunakan untuk membuat
routes.php
Route::get('/', function()
$products = Product::paginate(50);
});
views/tampilkan.blade.php
@foreach($products as $product)
<tr>
<td>
{{ Link::edit('product/edit/'.$product->id) }} |
{{ Link::delete('product/delete/'.$product->id) }}
</td>
</tr>
@endforeach
{{ $products->links() }}
Mudah bukan? dengan menggunakan Laravel pembuatan
dibawah ini.
folder baru, pada app/views. Misal, folder tersebut anda beri nama
structure. Anda boleh memberi nama lain jika anda mau. Setelah
berikut:
<?php
}}">
</a>
@for ($i = 1; $i <= $paginator->getLastPage(); $i++)
{{ $i }}
</a>
@endfor
</a>
</ul>
@endif
?>
mencoba!