PHP code example of ritechoice23 / laravel-saveable
1. Go to this page and download the library: Download ritechoice23/laravel-saveable 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/ */
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Ritechoice23\Saveable\Traits\HasSaves;
class User extends Authenticatable
{
use HasSaves;
// ...
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Ritechoice23\Saveable\Traits\IsSaveable;
class Post extends Model
{
use IsSaveable;
// ...
}
$user->saveItem($post);
$user->unsaveItem($post);
$user->toggleSaveItem($post);
if ($user->hasSavedItem($post)) {
// User has saved this post
}
if ($post->isSavedBy($user)) {
// This post is saved by the user
}
$collection = $user->collections()->where('name', 'Reading List')->first();
// Save the post directly into the 'Reading List' collection
$user->saveItem($post, $collection);
$collections = $user->collections;
$rootCollections = $user->rootCollections;
$subCollections = $collection->children;
$parent = $subCollection->parent;
$items = $collection->items(); // Returns a collection of Post, Product models, etc.
// Move to another collection
$user->moveSavedItem($post, $otherCollection);
// Move to root (unsorted)
$user->moveSavedItem($post, null);
$saves = $user->savedRecords;
// Get all saved posts
$savedPosts = $user->savedItems(Post::class)->where('published', true)->get();
// Or use savedItemsOfType for clarity
$savedPosts = $user->savedItemsOfType(Post::class)->latest()->get();
// Get Save records
$unsortedSaves = $user->unsortedSavedRecords();
// Get actual models (single type)
$unsortedPosts = $user->unsortedSavedItems(Post::class)->get();
// Get all unsorted (mixed types)
$allUnsorted = $user->unsortedSavedItems()->get();
// Get as a query (can chain additional filters)
$users = $post->savers(User::class)->where('verified', true)->get();
// Get grouped by type
$saversGrouped = $post->saversGrouped();
// Count all saved items
$totalCount = $user->savedItemsCount();
// Count by type
$postCount = $user->savedItemsCount(Post::class);
$user->saveItem($post, $collection, [
'note' => 'Read this by Friday.',
'priority' => 'high'
]);
$posts = Post::withSaveStatus($currentUser)->get();
foreach ($posts as $post) {
if ($post->is_saved) {
echo "Saved with metadata: " . json_encode($post->save_metadata);
}
}
$savedPosts = Post::whereSavedBy($user)->get();
$users = User::whereSavedItem($post)->get();
// Controller
class BookmarkController extends Controller
{
public function store(Post $post, Request $request)
{
$collection = null;
if ($request->collection_id) {
$collection = auth()->user()->collections()->find($request->collection_id);
}
auth()->user()->saveItem($post, $collection, [
'note' => $request->note
]);
return back()->with('success', 'Post bookmarked!');
}
public function destroy(Post $post)
{
auth()->user()->unsaveItem($post);
return back()->with('success', 'Bookmark removed!');
}
}
public function dashboard()
{
$user = auth()->user();
// Get all collections with item counts
$collections = $user->collections()
->withCount('saves')
->get();
// Get unsorted saved posts
$unsortedPosts = $user->unsortedSavedItems(Post::class)
->where('published', true)
->latest()
->get();
// Get all saved items grouped by type
$savedItemsGrouped = $user->savedItemsGrouped();
return view('dashboard', compact('collections', 'unsortedPosts', 'savedItemsGrouped'));
}
public function popular()
{
$popularPosts = Post::mostSaved(20)
->with('saveRecords')
->get();
return view('popular', compact('popularPosts'));
}
public function mySavedPosts()
{
// Using the Builder pattern - can chain any query methods
$savedPosts = auth()->user()
->savedItems(Post::class)
->where('published', true)
->where('category_id', 5)
->latest()
->paginate(20);
return view('my-saves', compact('savedPosts'));
}