Skip to content

5. Managing Users Items

Jesus Baron edited this page Jul 17, 2022 · 4 revisions

Adding/Updating Records

This package provides two ways for adding new records to the database, either by using the facade or by calling the recommendable method on a model instance or an Eloquent collection.

Note: If a record already exists in Recombee, it will be updated with the new values given.

Adding via Eloquent Model

Once you have added the Baron\Recombee\Recommendable trait to a model, all you need to do is call the recommendable method on a model instance and it will automatically be added to your database.

// Adding a single model
User::first()->recommendable();

// Adding a collection of models
Item::where('active', true)->get()->recommendable();

Adding via Facade

The Recombee facade provides a user and item methods that you can use to manually add a record into Recombee. You can either pass a model instance or an ID and its data (optionally):

use  Baron\Recombee\Facades\Recombee;

// Passing model instance
$model = User::first();
Recombee::user($model)->save();

// Passing raw data
Recombee::item(1, [
    'description' => '4K Screen TV',
    'price' => 209.99
])->save();

In some cases, depending on your application, it could be valuable to also add guest users to your Recombee database, you can do this by passing a unique token as the user's ID, such as the session ID:

use  Baron\Recombee\Facades\Recombee;

Recombee::user('guest-user-session-id')->save();

Removing Records

Removing via Eloquent Model

To remove a record from Recombee you may simply call the unrecommendable method on the model instance.

// Remove a single model
User::first()->unrecommendable();

// Remove a collection of models
Item::where('active', true)->get()->unrecommendable();

Removing via Facade

The Recombee facade provides a user and item methods that you can use to manually remove a record from Recombee using the delete method. You can either pass a model instance or an ID:

use  Baron\Recombee\Facades\Recombee;

// Passing model instance
$model = User::first();
Recombee::user($model)->delete();

// Passing raw ID
Recombee::item(1)->delete();

Merging Users

This package provides a mergeTo method for merging the interactions of two different users under a single user ID, this is especially useful for online e-commerce applications working with guest users identified by unique tokens such as the session ID.

Please visit Recombee API docs for more information on merging users.

use  Baron\Recombee\Facades\Recombee;

$targetUser = User::first();
Recombee::user('guest-user-session-id')->mergeTo($targetUser)->save();

Retrieve Users/Items

You may retrieve a collection of existing users or items by using the get method.

In addition to retrieving a collection of records, you may paginate your results using the paginate method. This method will return an Illuminate\Pagination\Paginator instance just as if you had simple paginated a traditional Eloquent query:

use  Baron\Recombee\Facades\Recombee;

Recombee::user()->get();
Recombee::user()->paginate();

Customize number of results

Results are limited to 25 entries by default, but you may chain the take method to customize the number of results. For pagination results, you can pass the number as an argument in the paginate method.

use  Baron\Recombee\Facades\Recombee;

Recombee::user()->take(50)->get();
Recombee::user()->paginate(50);

Customize fields returned

By default, all the user/item properties are returned in the response, you may customize the properties to return by chaining the select method and passing the property names as multiple arguments.

use  Baron\Recombee\Facades\Recombee;

Recombee::user()->get();                              # It will return all the properties by default.
Recombee::user()->select()->get();                    # It will return the ID only.
Recombee::user()->select('name', 'age')->paginate();  # It will return the ID, name, and age properties.

Passing Additional Options

Recombee can accept additional options to customize the results even further, for example, you may send the filter option which accepts a ReQL expression that allows you to filter users to be listed. This and any other option can be added to the query by chaining the option method which takes the option name as the 1st argument and its value as the 2nd argument.

Please visit the Recombee API Reference for all available options.

use  Baron\Recombee\Facades\Recombee;

Recombee::user()
    ->select('name', 'active', 'age')
    ->take(50)
    ->option('filter', "'age' > 25")
    ->paginate();