PHP code example of calebdw / laravel-sql-entities
1. Go to this page and download the library: Download calebdw/laravel-sql-entities library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
calebdw / laravel-sql-entities example snippets
namespace Database\Entities\Views;
use App\Models\Order;
use CalebDW\SqlEntities\View;
use Illuminate\Database\Query\Builder;
use Override;
// will create a view named `recent_orders_view`
class RecentOrdersView extends View
{
#[Override]
public function definition(): Builder|string
{
return Order::query()
->select(['id', 'customer_id', 'status', 'created_at'])
->where('created_at', '>=', now()->subDays(30))
->toBase();
// could also use raw SQL
return <<<'SQL'
SELECT id, customer_id, status, created_at
FROM orders
WHERE created_at >= NOW() - INTERVAL '30 days'
SQL;
}
}
use Illuminate\Database\Connection;
class RecentOrdersView extends View
{
// ...
#[Override]
public function creating(Connection $connection): bool
{
if (/** should not create */) {
return false;
}
/** other logic */
return true;
}
#[Override]
public function created(Connection $connection): void
{
$this->connection->statement(<<<SQL
GRANT SELECT ON TABLE {$this->name()} TO other_user;
SQL);
}
#[Override]
public function dropping(Connection $connection): bool
{
if (/** should not drop */) {
return false;
}
/** other logic */
return true;
}
#[Override]
public function dropped(Connection $connection): void
{
/** logic */
}
}
class RecentOrdersView extends View
{
#[Override]
public function dependencies(): array
{
return [OrdersView::class];
}
}
class RecentOrdersView extends View
{
// to create a recursive view
protected bool $recursive = true;
// adds a `WITH CHECK OPTION` clause to the view
protected string|true|null $checkOption = 'cascaded';
// can provide explicit column listing
protected ?array $columns = ['id', 'customer_id', 'status', 'created_at'];
}
namespace Database\Entities\Functions;
use CalebDW\SqlEntities\Function_;
class Add extends Function_
{
/** If the function aggregates. */
protected bool $aggregate = false;
protected array $arguments = [
'integer',
'integer',
];
/** The language the function is written in. */
protected string $language = 'SQL';
/** The function return type. */
protected string $returns = 'integer';
#[Override]
public function definition(): string
{
return <<<'SQL'
RETURN $1 + $2;
SQL;
}
}
namespace Database\Entities\Functions;
use CalebDW\SqlEntities\Function_;
class Add extends Function_
{
protected array $arguments = [
'integer',
'integer',
];
/** The language the function is written in. */
protected string $language = 'c';
protected bool $loadable = true;
/** The function return type. */
protected string $returns = 'integer';
#[Override]
public function definition(): string
{
return 'c_add';
}
}
namespace Database\Entities\Triggers;
use CalebDW\SqlEntities\Trigger;
class AccountAuditTrigger extends Trigger
{
// if the trigger is a constraint trigger
// PostgreSQL only
protected bool $constraint = false;
protected string $timing = 'AFTER';
protected array $events = ['UPDATE'];
protected string $table = 'accounts';
#[Override]
public function definition(): string
{
return $this->definition ?? <<<'SQL'
EXECUTE FUNCTION record_account_audit();
SQL;
}
}
use CalebDW\SqlEntities\Facades\SqlEntity;
use CalebDW\SqlEntities\SqlEntityManager;
use CalebDW\SqlEntities\View;
// Create a single entity by class or instance
SqlEntity::create(RecentOrdersView::class);
resolve(SqlEntityManager::class)->create(RecentOrdersView::class);
resolve('sql-entities')->create(new RecentOrdersView());
// Similarly, you can drop a single entity using the class or instance
SqlEntity::drop(RecentOrdersView::class);
// Create or drop all entities
SqlEntity::createAll();
SqlEntity::dropAll();
// You can also filter by type or connection
SqlEntity::createAll(types: View::class, connections: 'reporting');
SqlEntity::dropAll(types: View::class, connections: 'reporting');
use CalebDW\SqlEntities\Facades\SqlEntity;
use Illuminate\Database\Connection;
SqlEntity::withoutEntities(function (Connection $connection) {
$connection->getSchemaBuilder()->table('orders', function ($table) {
$table->renameColumn('old_customer_id', 'customer_id');
});
});
use CalebDW\SqlEntities\Facades\SqlEntity;
use Illuminate\Database\Connection;
SqlEntity::withoutEntities(
callback: function (Connection $connection) {
$connection->getSchemaBuilder()->table('orders', function ($table) {
$table->renameColumn('old_customer_id', 'customer_id');
});
},
types: [RecentOrdersView::class, RecentHighValueOrdersView::class],
connections: ['reporting'],
);
use CalebDW\SqlEntities\Listeners\SyncSqlEntities;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Event::subscribe(SyncSqlEntities::class);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.