Skip to content

Instantly share code, notes, and snippets.

@johnadan
Created June 1, 2023 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnadan/91d0ca06e7fb3dc736c06cea328059a2 to your computer and use it in GitHub Desktop.
Save johnadan/91d0ca06e7fb3dc736c06cea328059a2 to your computer and use it in GitHub Desktop.
laraval mysql view
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->down();
DB::statement("
CREATE VIEW view_field_managers AS
(
select
u.id,
u.username,
r.name as role,
replace(
concat_ws(
' ',
i.first_name,
i.middle_name,
i.last_name
),
' ',
' '
) as full_name,
i.first_name,
i.middle_name,
i.last_name,
l.created_at as last_log_in,
u.status
from
users u,
user_info i,
roles r,
role_user ru,
(select *
from user_logs l
where created_at =
(select max(created_at)
from user_logs
where l.user_id = user_id and activity = 'Logged In')) l
where
u.id = i.user_id
and u.id = ru.user_id
and ru.role_id = r.id
and u.id = l.user_id
and r.name = 'Field Manager'
order by id
)
");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('DROP VIEW IF EXISTS view_field_managers');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment