PHP code example of maduser / laravel-viewmodel

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

    

maduser / laravel-viewmodel example snippets

 
use Maduser\Laravel\ViewModel\ViewModel;

class MyQuoteWidget extends ViewModel
{
    protected $view = 'my-widget'; // Blade template
    protected $quote; // Quote string

    public function getQuote(): ?string
    {
        return $this->quote;
    }

    public function setQuote(?string $quote): MyQuoteWidget
    {
        $this->quote = $quote;
        return $this;
    }
}

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Maduser\Laravel\ViewModel\ViewModels\Page;

class ExampleController extends Controller
{
    private $exampleVars;

    public function __construct()
    {
        $this->exampleVars = [
            'title' => 'Welcome Home',
            'text' => 'An inspiring quote here'
        ];
    }

    public function showPage(): Responsable
    {
        // Creating and returning a ViewModel instance
        return Page::create($this->exampleVars);
    }
}


$userWidget = UserWidget::create([
    'profile' => UserProfile::create(),
    'activity' => UserActivity::create(['activities' => $user->activities])
]);