PHP code example of axn / laravel-view-components

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

    

axn / laravel-view-components example snippets


// config/app.php

'provider' => [
    //...
    Axn\ViewComponents\ServiceProvider::class,
    //...
];

namespace App\ViewComponents;

use Axn\ViewComponents\ViewComponent;
use App\Models\User;
use LaravelCollective\FormFacade as Form;

class UsersSelect extends ViewComponent
{
    protected function compose()
    {
        $users = User::pluck('username', 'id');

        return Form::select('user_id', $users, null, [
            'class' => 'form-control'
        ]);
    }
}

$content = render(App\ViewComponents\UsersSelect::class);

$content = render(App\ViewComponents\UsersSelect::class, [
    'selectedUser' => old('user_id', $currentUser->id)
]);

//...

class UsersSelect extends ViewComponent
{
    protected function compose()
    {
        $users = User::pluck('username', 'id');

        // $this->selectedUser value comes from the parent view
        return Form::select('user_id', $users, $this->selectedUser, [
            'class' => 'form-control'
        ]);
    }
}
blade
<div class="form-group">
    {!! Form::label('user_id', 'Select a user:') !!}
    @render(App\ViewComponents\UsersSelect::class)
</div>
blade
<div class="form-group">
    {!! Form::label('user_id', 'Select a user:') !!}
    @render(App\ViewComponents\UsersSelect::class, [
        'selectedUser' => old('user_id', $currentUser->id)
    ])
</div>