Assalamualaikum temen-temen
Heyyoo wasap guys balik lagi di blog mimin yak, kali ini mimin akan berbagi bagaimana sih caranya melakukan migrasi pada Laravel, yuk ah cuss..
1. Kalian harus mencari path PHP nya biasanya ada di dalam folder xampp kalian, lalu copy lokasi filenya (yang d beri tanda merah oleh mimin)
2. Konfigurasi PATH PHP nya, kalian pergi ke File Explorer kemudian klik kanan This PC pilih Properties dan klik Advanced system settings lalu klik tombol Environment Variables.. dan cari di System variables namanya PATH lalu klik edit dan paste kan lokasi path php kalian dan simpan
3. Cek konfigurasi PATH PHP kalian di CMD dengan mengetik php -v
4. Dan terakhir edit file
.env pada root folder project laravel kalian,
konfigurasi
koneksi
database di file tersebut
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_belajarlaravel
DB_USERNAME=root
DB_PASSWORD=
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_belajarlaravel
DB_USERNAME=root
DB_PASSWORD=
Nah setelah itu langsung aja ke studi kasus nya yuk
>> Studi kasus 1 <<
Buatlah
migrasi
baru
untuk
membuat
tabel
t_percobaan.
Cobalah
buat
minimal 15 field yang berbeda
pada tabel
tersebut
Oke pertama tama kita pergi ke cmd dan ketik php artisan make:migration create_t_ujicoba untuk membuat tabel baru dan jika berhasil maka akan muncul sebuah tulisan Created Migration: 2019_02_23_114005_create_t_ujicoba seperti dibawah ini:
D:\xampp2\htdocs\blog>php artisan make:migration create_t_ujicoba
Created Migration: 2019_02_23_114005_create_t_ujicoba
Created Migration: 2019_02_23_114005_create_t_ujicoba
Kemudian buka file 2019_02_23_114005_create_t_ujicoba lalu edit untuk menambahkan field baru seperti dibawah:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTUjicoba extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('t_ujicoba', function (Blueprint $table) {
$table->increments('id');
$table->integer('nim');
$table->string('nama');
$table->string('tempat_lahir');
$table->date('tanggal_lahir');
$table->string('agama');
$table->string('jen_kel');
$table->string('alamat');
$table->integer('telepon');
$table->string('kd_jurusan');
$table->string('kd_matkul');
$table->string('nama_ibu');
$table->string('nama_ayah');
$table->string('pekerjaan_ayah');
$table->string('pekerjaan_ibu');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTUjicoba extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('t_ujicoba', function (Blueprint $table) {
$table->increments('id');
$table->integer('nim');
$table->string('nama');
$table->string('tempat_lahir');
$table->date('tanggal_lahir');
$table->string('agama');
$table->string('jen_kel');
$table->string('alamat');
$table->integer('telepon');
$table->string('kd_jurusan');
$table->string('kd_matkul');
$table->string('nama_ibu');
$table->string('nama_ayah');
$table->string('pekerjaan_ayah');
$table->string('pekerjaan_ibu');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Setelah itu kita ke cmd lagi lalu ketikan php artisan migrate dan jika berhasil maka akan muncul seperti di bawah:
D:\xampp2\htdocs\blog>php artisan migrate
Migrating: 2019_02_23_114005_create_t_ujicoba
Migrated: 2019_02_23_114005_create_t_ujicoba
Migrating: 2019_02_23_114005_create_t_ujicoba
Migrated: 2019_02_23_114005_create_t_ujicoba
Tadaa berhasil membuat 15 field di tabel ujicoba
>> Studi kasus 2 <<
1.Buatlah
tabel
t_kelas dengan
field nama_kelas,
jurusan
2.Buatlah
perubahan
tabel
t_kelas dengan
menambahkan
field lokasi_ruangan dengan
tipe
data string dan panjang
100 karakter
3.Buatlah
perubahan
tabel
t_kelas
dengan
mengganti
panjangnya
menjadi
50 karakter
4.Buatlah
perubahan
tabel t_kelas
dengan
menambahkan
field nama_walkel
dengan
tipe
data string dan panjang
50 karakter
5.Buatlah
perubahan
tabel
t_kelas dengan
mengubah
nama
field nama_walkel menjadi
nama_wali_kelas.
Jawab:
1. Buat t_kelas
D:\xampp2\htdocs\blog>php artisan make:migration create_t_kelas
Created Migration: 2019_02_24_043855_create_t_kelas
Created Migration: 2019_02_24_043855_create_t_kelas
Edit filenya:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTKelas extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//1. Membuat field tabel t_kelas
Schema::create('t_kelas', function (Blueprint $table){
$table->increments('id');
$table->string('nama_kelas');
$table->string('jurusan');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTKelas extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//1. Membuat field tabel t_kelas
Schema::create('t_kelas', function (Blueprint $table){
$table->increments('id');
$table->string('nama_kelas');
$table->string('jurusan');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Ke cmd lagi
D:\xampp2\htdocs\blog>php artisan migrate
Migrating: 2019_02_24_043855_create_t_kelas
Migrated: 2019_02_24_043855_create_t_kelas
Migrating: 2019_02_24_043855_create_t_kelas
Migrated: 2019_02_24_043855_create_t_kelas
Jika berhasil maka akan muncul di phpmyadmin
2. Menambah field lokasi_ruangan
D:\xampp2\htdocs\blog>php artisan make:migration add_lokasi_ruangan
Created Migration: 2019_02_24_044048_add_lokasi_ruangan
Created Migration: 2019_02_24_044048_add_lokasi_ruangan
Edit filenya:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLokasiRuangan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//2. Menambahkan field lokasi ruangan pada table t_kelas
Schema::table('t_kelas',function($table){
$table->string('lokasi_ruangan', 100)->after('jurusan');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('t_kelas', function($table){
$table->dropColumn('lokasi_ruangan');
});
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLokasiRuangan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//2. Menambahkan field lokasi ruangan pada table t_kelas
Schema::table('t_kelas',function($table){
$table->string('lokasi_ruangan', 100)->after('jurusan');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('t_kelas', function($table){
$table->dropColumn('lokasi_ruangan');
});
}
}
Ke cmd lagi
D:\xampp2\htdocs\blog>php artisan migrate
Migrating: 2019_02_24_044048_add_lokasi_ruangan
Migrated: 2019_02_24_044048_add_lokasi_ruangan
Migrating: 2019_02_24_044048_add_lokasi_ruangan
Migrated: 2019_02_24_044048_add_lokasi_ruangan
Jika berhasil maka akan muncul
3. Mengganti panjang menjadi 50
Ke cmd
D:\xampp2\htdocs\blog>php artisan make:migration change_length_of_t_kelas
Created Migration: 2019_02_24_044311_change_length_of_t_kelas
Created Migration: 2019_02_24_044311_change_length_of_t_kelas
Edit filenya:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeLengthOfTKelas extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//3. Edit data kelas
Schema::table('t_kelas', function (Blueprint $table){
$table->string('nama_kelas',50)->change();
$table->string('jurusan',50)->change();
$table->string('lokasi_ruangan',50)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeLengthOfTKelas extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//3. Edit data kelas
Schema::table('t_kelas', function (Blueprint $table){
$table->string('nama_kelas',50)->change();
$table->string('jurusan',50)->change();
$table->string('lokasi_ruangan',50)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Ke cmd lagi:
D:\xampp2\htdocs\blog>php artisan migrate
Migrating: 2019_02_24_044311_change_length_of_t_kelas
Migrated: 2019_02_24_044311_change_length_of_t_kelas
Migrating: 2019_02_24_044311_change_length_of_t_kelas
Migrated: 2019_02_24_044311_change_length_of_t_kelas
Dan lihat perubahannya d phpmyadmin
4. Menambah field nama_walkel
Ke cmd
D:\xampp2\htdocs\blog>php artisan make:migration add_nama_walkel
Created Migration: 2019_02_24_045046_add_nama_walkel
Created Migration: 2019_02_24_045046_add_nama_walkel
Edit filenya:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNamaWalkel extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//4. Menambahkan field nama_walkel
Schema::table('t_kelas',function($table){
$table->string('nama_walkel', 50)->after('lokasi_ruangan');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('t_kelas', function($table){
$table->dropColumn('nama_walkel');
});
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNamaWalkel extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//4. Menambahkan field nama_walkel
Schema::table('t_kelas',function($table){
$table->string('nama_walkel', 50)->after('lokasi_ruangan');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('t_kelas', function($table){
$table->dropColumn('nama_walkel');
});
}
}
Ke cmd lagi:
D:\xampp2\htdocs\blog>php artisan migrate
Migrating: 2019_02_24_045046_add_nama_walkel
Migrated: 2019_02_24_045046_add_nama_walkel
Migrating: 2019_02_24_045046_add_nama_walkel
Migrated: 2019_02_24_045046_add_nama_walkel
Dan lihat perubahannya d phpmyadmin
Ke cmd
D:\xampp2\htdocs\blog>php artisan make:migration change_nama_walkel_t_kelas
Created Migration: 2019_02_24_045704_change_nama_walkel_t_kelas
Created Migration: 2019_02_24_045704_change_nama_walkel_t_kelas
Edit filenya:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeNamaWalkelTKelas extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//5. Mengubah nama wali kelas
Schema::table('t_kelas',function($table){
$table->renameColumn('nama_walkel','nama_wali_kelas');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('t_kelas',function($table){
$table->renameColumn('nama_walkel','nama_wali_kelas');
});
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeNamaWalkelTKelas extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//5. Mengubah nama wali kelas
Schema::table('t_kelas',function($table){
$table->renameColumn('nama_walkel','nama_wali_kelas');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('t_kelas',function($table){
$table->renameColumn('nama_walkel','nama_wali_kelas');
});
}
}
Ke cmd lagi:
D:\xampp2\htdocs\blog>php artisan migrate
Migrating: 2019_02_24_045704_change_nama_walkel_t_kelas
Migrated: 2019_02_24_045704_change_nama_walkel_t_kelas
Migrating: 2019_02_24_045704_change_nama_walkel_t_kelas
Migrated: 2019_02_24_045704_change_nama_walkel_t_kelas
Dan lihat perubahannya d phpmyadmin
Nah itu dia pertemuan kali ini gaes, mohon maaf bila ada banyak kesalahan sampai jumpa di lain waktu.. daah
0 Komentar