Create chain select dropdown using Laravel, Laravolt and Liveware

Install Laravolt Indonesia package

composer require laravolt/indonesia

php artisan vendor:publish --provider="Laravolt\Indonesia\ServiceProvider"
php artisan migrate
php artisan laravolt:indonesia:seed

Install Livewire

composer require livewire/livewire

Create livewire dropdown component

php artisan make:livewire ChainDropdown

Setup routes

Route::get('/dropdown', [HomeController::class,'render_dropdown']);

Setup controller and views

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    //other functions...

    public function render_dropdown()
    {
      return view('layouts.dropdown'); 
    }
}

@extends('layouts.app')

@section('content')
        <div class="container">
            <livewire:chain-dropdown />
        </div>
@endsection

Setup livewire class component

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class ChainDropdown extends Component
{
    public $provinces, $cities;
    public $provinceId=null;

    public function mount()
    {
        $this->provinces = \Indonesia::allProvinces();
        $this->cities = collect();
    }

    public function render()
    {
        return view('livewire.chain-dropdown');
    }

    public function updatedProvinceId($province_id)
    {
        if(!is_null($province_id)){
            $this->cities = \Indonesia::findProvince($province_id, ['cities']);
        }
    }
}

Setup livewire views component

<div>
    <select name="provices" id="provinces" wire:model="provinceId">
        <option value="">Select Province</option>
        @foreach ($provinces as $province)
            <option value="{{ $province->id }}">{{ $province->name }}</option>
        @endforeach
    </select>
    @if (!is_null($provinceId))
        <select>
            <option value="" selected>Select City</option>
            @foreach ($cities->cities as $city)
                <option value="{{ $city->id }}">{{ $city->name }}</option>
            @endforeach
        </select>
    @endif

</div>

full source code can you view on my github https://github.com/dwijonarko/laravel-8-module

Customize Fortify : Login Using username instead email

Create migration to add username column on users table

php artisan make:migration addUsernameOnUsersTable

Add this code on yout migration code

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUsernameOnUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users',function(Blueprint $table){
            $table->string('username')->after('name');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('username');
        });     
    }
}

Run the migration

php artisan migrate

Change field email to username.

Change in app/fortify.php ‘username’=>’email’ to ‘usename’=>’username’ (line number estimate 49)

Change in FortifyServiceProvider.php

Fortify::authenticateUsing(function (Request $request) {
            $user = User::where('username', $request->username)->first();

            if (
                $user &&
                Hash::check($request->password, $user->password)
            ) {
                return $user;
            }
        });

[Iseng] Nyoba Laravel Sail

Laravel 8 banyak membawa perubahan (kalo ga mau disebut magic) dalam rilis kali ini https://laravel.com/docs/8.x/releases kalo yang baru berangkat dari versi sebelumnya, kemudian mencoba versi ini, kalo ga baca dengan teliti pasti akan kebingungan. Selain itu, versi ini juga membawa jargon-jargon baru, contohnya saja Sail (padahal Docker), Breeze (untuk generate basic auth function) dan Jetstream ( advance auth function).

Nah kali ini saya penasaran coba Laravel Sail, salah satu cara instalasi framework Laravel yang menggunakan Docker. Dikutip dari laman aslinya

If you’re developing on Linux and Docker is already installed, you can use a simple terminal command to create a new Laravel project

https://laravel.com/docs/8.x/installation#getting-started-on-linux

Kebetulan, semenjak belajar Docker, di komputer saya sudah mulai meninggalkan instalasi apache,mysql dkk langsung. Sebelum ini saya pake laradock untuk menjalankan Laravel.

Continue reading

Mengenal Laravel Jetstream di Laravel 8

Bagi yang mengikuti perkembangan laravel mulai versi 5-7, kita mengenal fitur laravel untuk melakukan generate fungsi auth (login, register, logout) pada aplikasi laravel. Perintah yang digunakan adalah laravel make:auth, kemudian berkembang menggunakan packaga laravel/ui, pada laravel versi terbaru versi 8, terdapat package baru untuk melakukan generate tersebut menggunakan jetstream.

Continue reading