User

Introduction

The Assely framework comes with App\Users repository class that allows for managing WordPress users.

This singularity, unlike other, is registered by default inside App\Providers\SingularityServiceProvider class.

Columns on the list of Users

The columns method inside the app\Users.php file allows for defining custom columns which are displayed on the list of users.

Users accept one kind of column: user.

public function columns()
{
    return [
        Column::user('App\Profiles\MovieCritic', 'score'),
    ];
}

Using Users Repository

User facade provides access to the users. However, you can also inject the App\Users repository class.

use App\Users;

public function index(Users $users) {
    $user = $users->find(1);
}

Quering User

In this section, you will learn how you can query users of your application.

Fetching multiple Users

The all method will retrieve all of the users. This method returns an instance of Illuminate\Support\Collection, so you can easily modify results with different helper methods.

$users = User::all();

Retrieving single User

You can also immediately search for a single user. Pass user id as the find method argument.

$user = User::find(1);
Finding Users by a given fields

The findBy method allows for finding users by they profile field. You can use id, slug, login or even email.

User::findBy('email', 'user@example.com')
Not Found Query Exception

Sometimes you want to throw an exception when a user was not found. The findOrFail method returns the found user, but if not, the Assely\Database\QueryException will be thrown.

User::findOrFail(1);

Custom Query

Needs something more complex? You can use query method with an array of parameters as the argument.

User::query([
    'date_query' => [
        [
            'after' => '12 hours ago',
            'inclusive' => true
        ]
    ]
]);

Inserting Users

Use create method with an array of properties and values for inserting new user to the database.

User::create([
    'email' => 'john.smith@example.com',
]);
Inserting Query Exception

The createdOrFail method will insert a new user, but when an error occurs, the Assely\Database\QueryException will be thrown.

User::createOrFail([
    'email' => 'john.smith@example.com',
]);

Updating Users

To update a user, you need to set new property values on retrieved Assely\Adapter\User instance, and then call save method.

$user = User::find(1);

$user->name = 'John Smith';

$user->save();

Deleting User

Call destroy method on retrieved Assely\Adapter\User instance to remove a user from the database.

$user = User::find(1);

$user->destroy();

Working with User roles

WordPress users can have different roles and capabilities. You can verify what users can and cannot do.

Getting User roles
$user_roles = User::find(1)->roles;
Verifing User roles

With hasRole method you can check if a user has the specifed role.

$user = User::find(1);

if ($user->hasRole('Administrator')) {
    // Yes, he is an administrator.
}
Verifing User capabilities

You can verify what users can and cannot do with can method. Reference to the Codex for a list of all available capabilities.

$user = User::find(1);

if ($user->can('edit_posts')) {
    // Yes, he can edit posts.
}

Rendering Users in templates

You have access to the bunch of different properties. List of all you can find in adapters documentation.

<div class="user">
    <h2>{{ $user->name }}</h2>
    <span>{{ $user->email }}</span>
</div>

Accessing Users metadata

Your users may hold various metadata. The meta method helps you retrieve this information. You only need to pass metadata key under which they are stored.

{{ $user->meta('score') }}