1. Go to this page and download the library: Download wakebit/laravel-cycle 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/ */
wakebit / laravel-cycle example snippets
declare(strict_types=1);
namespace App\Entity;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Column;
#[Entity]
class User
{
#[Column(type: 'primary')]
protected int $id;
#[Column(type: 'string')]
protected string $name;
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
declare(strict_types=1);
namespace App\Http\Controllers;
use Cycle\Database\DatabaseProviderInterface;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\ORMInterface;
use Illuminate\Http\Response;
final class HomeController
{
public function __construct(
private DatabaseProviderInterface $dbal,
private EntityManagerInterface $em,
private ORMInterface $orm,
) {
}
public function __invoke()
{
// DBAL
$tables = $this->dbal->database()->getTables();
$tableNames = array_map(fn (\Cycle\Database\TableInterface $table): string => $table->getName(), $tables);
dump($tableNames);
// Create, modify, delete entities using Transaction
$user = new \App\Entity\User();
$user->setName("Hello World");
$this->em->persist($user);
$this->em->run();
dump($user);
// ORM
$repository = $this->orm->getRepository(\App\Entity\User::class);
$users = $repository->findAll();
dump($users);
$user = $repository->findByPK(1);
dump($user);
}
}