PHP code example of visavi / motor

1. Go to this page and download the library: Download visavi/motor 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/ */

    

visavi / motor example snippets


class Story extends Model
{
    protected string $table = 'stories.csv';

    protected array $casts = [
        'active'     => 'bool',
        'rating'     => 'int',
        'user_id'    => 'int',
        'created_at' => 'int',
    ];

    public function user(): Relation
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function comments(): Relation
    {
        return $this->hasMany(Comment::class);
    }

    public function getLink(): string
    {
        return route('story-view', ['slug' => sprintf('%s-%d', $this->slug, $this->id)]);
    }
}

$stories = Story::query()
    ->active()
    ->where('created_at', '<', time())
    ->orderByDesc('created_at')
    ->with(['user', 'comments'])
    ->paginate(setting('story.per_page'));

foreach ($stories as $story) {
    echo $story->getLink(), $story->user->getName();
}

class StoryController extends Controller
{
    public function __construct(
        protected View $view,
        protected StoryRepository $storyRepository,
    ) {}

    public function index(Response $response): Response
    {
        $stories = $this->storyRepository->getStories(setting('story.per_page'));

        return $this->view->render($response, 'stories/index', compact('stories'));
    }
}

use App\Models\Comment;
use MotorORM\Migration;

return new class
{
    public function up(): void
    {
        new Migration(new Comment())
            ->create('parent_id')->default(0)->after('story_id')
            ->changeTable();
    }

    public function down(): void
    {
        new Migration(new Comment())
            ->delete('parent_id')
            ->changeTable();
    }
};

setting('story.per_page');
setting('main.allow_register');

<?= pagination($stories) 
bash
php motor migrate           # apply new migrations
php motor migrate:rollback  # roll the last batch back
php motor backup            # zip storage/database into storage/backups
php motor backup:restore <archive>   # put a backup back