Code Monkey home page Code Monkey logo

laravel-recently-viewed's Introduction

Laravel: Recently Viewed

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Add functionality to save/get in session recently viewed entities

You can track any number of entities. Each list will be saved separately.

Session storage (without persist)

For example:

"recently_viewed" => array:2 [
                      "App\Models\Product" => array:2 [
                        0 => 'a3cda131-e599-4802-84ea-a3dddc19fa8c'
                        1 => '4413b636-9752-43b3-8361-3ef38c27acf9'
                      ]
                      "App\Domain\Property" => array:3 [
                        0 => 133
                        1 => 134
                        2 => 653
                      ]
                    ]

Installation

You can install the package via composer:

composer require yaroslawww/laravel-recently-viewed

You can publish the config file with:

php artisan vendor:publish --provider="RecentlyViewed\ServiceProvider" --tag="config"

Configuration in .env

# Optional
# RECENTLY_VIEWED_SESSION_PREFIX=recently_viewed

Usage example

<?php
use Illuminate\Database\Eloquent\Model;
use RecentlyViewed\Models\Contracts\Viewable;
use RecentlyViewed\Models\Traits\CanBeViewed;

class Product extends Model implements Viewable
{
    // implement interface
    use CanBeViewed;
}
<?php
class ProductController extends Controller
 {
     public function show(Product $product)
     {
         \RecentlyViewed\Facades\RecentlyViewed::add($product);
 
         return view('my-view');
     }
 }
<?php
class ProductsViewComposer
{
    public function compose(View $view)
    {
        $view->with([
            'recentlyViewedProducts' => \RecentlyViewed\Facades\RecentlyViewed::get(Product::class),
            // or
            'recentlyViewedProductsWithoutLast' => \RecentlyViewed\Facades\RecentlyViewed::get(Product::class)->slice(1),
        ]);
        // or
        $view->with([
            'recentlyViewedProductsFiltered' => \RecentlyViewed\Facades\RecentlyViewed::getQuery(Product::class)
            ?->where('not_display_in_recently_list', false)->get()
            ??collect([]),
        ]);
    }
}

Add persist storage

You can enable migration and run the migrations with:

\RecentlyViewed\PersistManager::enableMigrations();
php artisan migrate
``

Configuration in *.env*

```dotenv
RECENTLY_VIEWED_PERSIST_ENABLED=true
use RecentlyViewed\Models\Contracts\Viewer;
use RecentlyViewed\Models\Traits\CanView;

class User extends Authenticatable implements Viewer
{
    use CanView;

    // ...
}

Add "merge" method after login (if you want merge saved data before login and already stored data)

class LoginController extends Controller
{
    // ...

    protected function authenticated(Request $request, $user)
    {
        \RecentlyViewed::mergePersistToCurrentSession();
    }
}

Credits

  • Think Studio

laravel-recently-viewed's People

Contributors

legend6 avatar michaellatham avatar snipiba avatar yaroslawww avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.