### Post Model Setup Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Example of a Post model implementing the OwnableContract and using the IsOwnable trait for ownership management. ```php use Illuminate\Database\Eloquent\Model; use App\Traits\IsOwnable; use App\Contracts\OwnableContract; class Post extends Model implements OwnableContract { use IsOwnable; protected $fillable = ['title', 'content', 'published_at']; } ``` -------------------------------- ### User Model Setup Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Example of a User model implementing the OwnerContract and using the HasOwnables trait for ownership management. ```php use Illuminate\Foundation\Auth\User as Authenticatable; use App\Traits\HasOwnables; use App\Contracts\OwnerContract; class User extends Authenticatable implements OwnerContract { use HasOwnables; protected $fillable = ['name', 'email', 'password']; } ``` -------------------------------- ### Creating and Assigning Posts Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Example demonstrating how to create a new post and assign ownership to a user, both directly and via the owner model. ```php // Create a new post and assign ownership $user = User::find(1); $post = Post::create([ 'title' => 'My First Blog Post', 'content' => 'This is the content of my blog post...', 'published_at' => now() ]); // Assign ownership $post->ownedBy($user); // Or using the owner model $user->giveOwnershipTo($post); ``` -------------------------------- ### Setup Owner and Ownable Models in Laravel Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Demonstrates how to implement the necessary traits and contracts for owner and ownable models in Laravel. ```php ownables()->get(); // Get current owner of an item $currentOwner = $post->currentOwner(); // Get all owners (including historical) $allOwners = $post->owners()->get(); // Get only current owners $currentOwners = $post->owners()->wherePivot('is_current', true)->get(); ``` -------------------------------- ### Check Ownership of an Item in Laravel Source: https://github.com/sowailem/ownable/blob/main/README.md These PHP examples demonstrate how to verify if a specific owner possesses an item. The checks can be performed using methods on both the owner and ownable models, or via the Owner facade. ```php // Check ownership if ($user->owns($post)) { // User owns this post } // Or if ($post->isOwnedBy($user)) { // Post is owned by this user } // Or using facade if (Owner::check($user, $post)) { // Check ownership using facade } ``` -------------------------------- ### Transfer Ownership Between Models in Laravel Source: https://github.com/sowailem/ownable/blob/main/README.md These PHP examples illustrate how to change the owner of an item from one owner to another. Transfers can be initiated from the current owner model, the ownable model, or by using the Owner facade. ```php // Transfer ownership $newOwner = User::find(2); $user->transferOwnership($post, $newOwner); // Or $post->transferOwnershipTo($newOwner); // Or using facade Owner::transfer($user, $newOwner, $post); ``` -------------------------------- ### Get All Owners (IsOwnable Trait) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Retrieves all owners associated with this ownable entity. This method is part of the IsOwnable trait. ```php /** * Get all owners of this ownable entity. * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function owners(): \Illuminate\Database\Eloquent\Relations\MorphToMany; ``` -------------------------------- ### Get Ownership Records (IsOwnable Trait) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Retrieves all ownership records associated with this ownable entity. This method is part of the IsOwnable trait. ```php /** * Get all ownership records for this ownable entity. * * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function ownerships(): \Illuminate\Database\Eloquent\Relations\MorphMany; ``` -------------------------------- ### Give Ownership to a Model in Laravel Source: https://github.com/sowailem/ownable/blob/main/README.md These PHP examples show different ways to grant ownership of an item to a specific owner. You can use the owner model's method, the ownable model's method, or the package's facade. ```php $user = User::first(); $post = Post::first(); // Give ownership $user->giveOwnershipTo($post); // Or $post->ownedBy($user); // Or using facade Owner::give($user, $post); ``` -------------------------------- ### Remove Ownership of an Item in Laravel Source: https://github.com/sowailem/ownable/blob/main/README.md This PHP example shows how to revoke ownership of an item from a specific owner. This action removes the ownership record associated with the given owner and item. ```php // Remove ownership $user->takeOwnershipFrom($post); ``` -------------------------------- ### Get Current Owner (IsOwnable Trait) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Retrieves the current owner of this ownable entity. Returns the owner model or null if no current owner exists. This method is part of the IsOwnable trait. ```php /** * Get the current owner of this ownable entity. * * @return \Illuminate\Database\Eloquent\Model|null The current owner or null if no current owner */ public function currentOwner(): ?\Illuminate\Database\Eloquent\Model; ``` -------------------------------- ### Publish and Run Laravel Ownable Migrations Source: https://github.com/sowailem/ownable/blob/main/README.md These commands publish the package's migration files and then run them to create the necessary database schema for tracking ownership. The first command makes the migration file available, and the second executes it. ```bash php artisan vendor:publish --provider="Sowailem\Ownable\OwnableServiceProvider" --tag="ownable-migrations" php artisan migrate ``` -------------------------------- ### E-commerce Product Management (PHP) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Demonstrates creating a product, assigning it to a vendor, retrieving all products for a vendor, and transferring product ownership to another vendor. ```php $vendor = Vendor::find(1); $product = Product::create([ 'name' => 'Wireless Headphones', 'description' => 'High-quality wireless headphones...', 'price' => 99.99, 'stock' => 50 ]); $product->ownedBy($vendor); // Get all products for a vendor $vendorProducts = $vendor->products()->get(); // Transfer product to another vendor $newVendor = Vendor::find(2); $product->transferOwnershipTo($newVendor); ``` -------------------------------- ### Set up Owner and Ownable Models with Traits Source: https://github.com/sowailem/ownable/blob/main/README.md This PHP code demonstrates how to set up your Eloquent models to use the Laravel Ownable package. It involves using the `HasOwnables` trait on the owner model and the `IsOwnable` trait on the model that will be owned. ```php use Sowailem\Ownable\Traits\HasOwnables; use Sowailem\Ownable\Traits\IsOwnable; use Sowailem\Ownable\Contracts\Ownable as OwnableContract; // Owner model (e.g., User) class User extends Authenticatable implements OwnerContract { use HasOwnables; } // Ownable model class Post extends Model implements OwnableContract { use IsOwnable; } ``` -------------------------------- ### Publish Laravel Ownable Configuration Source: https://github.com/sowailem/ownable/blob/main/README.md This command publishes the configuration file for the Laravel Ownable package, allowing you to customize settings like default model classes and database table names. After publishing, you can modify the configuration file. ```bash php artisan vendor:publish --provider="Sowailem\Ownable\OwnableServiceProvider" --tag="ownable-config" ``` -------------------------------- ### Basic Ownership Operations in Laravel Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Illustrates various methods for giving, checking, and transferring ownership of items between models using the Laravel Ownable package. ```php $user = User::first(); $post = Post::first(); // Giving Ownership $user->giveOwnershipTo($post); $post->ownedBy($user); Owner::give($user, $post); // Checking Ownership if ($user->owns($post)) { // User owns this post } if ($post->isOwnedBy($user)) { // Post is owned by this user } if (Owner::check($user, $post)) { // Check ownership using facade } // Transferring Ownership $newOwner = User::find(2); $user->transferOwnership($post, $newOwner); $post->transferOwnershipTo($newOwner); Owner::transfer($user, $newOwner, $post); ``` -------------------------------- ### E-commerce Vendor and Product Models (PHP) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Defines Eloquent models for Vendors and Products, implementing ownership contracts and traits. Includes methods for managing relationships and ownership. ```php class Vendor extends Model implements OwnerContract { use HasOwnables; protected $fillable = ['name', 'email', 'store_name']; public function products() { return $this->ownables()->where('ownable_type', Product::class); } } class Product extends Model implements OwnableContract { use IsOwnable; protected $fillable = ['name', 'description', 'price', 'stock']; public function vendor() { return $this->currentOwner(); } } ``` -------------------------------- ### Owner Facade Methods Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Methods available on the Owner facade for performing ownership operations. ```APIDOC ## Owner Facade ### Description Convenient facade for performing ownership operations. ### give($owner, $ownable) Assigns ownership of an item to a specific owner. #### Parameters - **owner** (Model) - Required - The model that will be the owner. - **ownable** (Model) - Required - The model that will be owned. ### check($owner, $ownable) Checks if a given owner owns a specific item. #### Parameters - **owner** (Model) - Required - The model to check as the owner. - **ownable** (Model) - Required - The model to check for ownership. #### Returns boolean ### transfer($fromOwner, $toOwner, $ownable) Transfers ownership of an item from one owner to another. #### Parameters - **fromOwner** (Model) - Required - The current owner of the item. - **toOwner** (Model) - Required - The new owner of the item. - **ownable** (Model) - Required - The item whose ownership is being transferred. ``` -------------------------------- ### Advanced Ownership Retrieval and Management in Laravel Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Shows how to retrieve owned items, current owners, historical owners, and remove ownership records using the Laravel Ownable package. ```php // Get all items owned by a user $ownedItems = $user->ownables()->get(); // Get current owner of an item $currentOwner = $post->currentOwner(); // Get all owners (including historical) $allOwners = $post->owners()->get(); // Get only current owners $currentOwners = $post->owners()->wherePivot('is_current', true)->get(); // Get ownership history $ownershipHistory = $post->ownerships()->with('owner')->get(); // Removing Ownership $user->takeOwnershipFrom($post); // This will delete the ownership record entirely ``` -------------------------------- ### IsOwnable Trait Methods Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Methods available on ownable models that implement the IsOwnable trait for managing ownership relationships. ```APIDOC ## IsOwnable Trait ### Description Methods available on ownable models that use the IsOwnable trait. ### currentOwner() Get the current owner of the ownable item. #### Returns \Illuminate\Database\Eloquent\Model ### owners() Get all owners (including historical) of the ownable item. #### Returns \Illuminate\Database\Eloquent\Relations\MorphToMany ### ownerships() Get the ownership history records for the ownable item. #### Returns \Illuminate\Database\Eloquent\Relations\MorphMany ``` -------------------------------- ### File Ownership Management with History (PHP) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Defines a File model that tracks ownership history. Includes a method to retrieve the ownership history of a file, showing who owned it and when. ```php class File extends Model implements OwnableContract { use IsOwnable; protected $fillable = ['name', 'path', 'size', 'mime_type']; public function getOwnershipHistory() { return $this->ownerships() ->with('owner') ->orderBy('created_at', 'desc') ->get(); } } // Usage $file = File::find(1); $history = $file->getOwnershipHistory(); foreach ($history as $ownership) { echo "Owned by: " . $ownership->owner->name . " from " . $ownership->created_at; } ``` -------------------------------- ### Give Ownership (Owner Facade) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Static method to give ownership of an ownable entity to a specified owner. Throws InvalidArgumentException for invalid parameters. ```php /** * Give ownership of an ownable entity to an owner. * * @param \App\Models\User $owner The owner model * @param \App\Models\Post $ownable The ownable entity * @return mixed The ownable entity with updated ownership * @throws \InvalidArgumentException when parameters are invalid */ public static function give(\Illuminate\Database\Eloquent\Model $owner, $ownable); ``` -------------------------------- ### Laravel Ownable HasOwnables Trait Methods Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Details the methods available within the HasOwnables trait for owner models, including retrieving possessions and ownables. ```php // possessions() // Get all ownership records where this model is the owner. // Returns: \Illuminate\Database\Eloquent\Relations\MorphMany // ownables() // Get all ownable entities owned by this owner. // Returns: \Illuminate\Database\Eloquent\Relations\MorphToMany ``` -------------------------------- ### Transfer Ownership (Owner Facade) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Static method to transfer ownership of an ownable entity from one owner to another. Throws InvalidArgumentException for invalid parameters. ```php /** * Transfer ownership of an ownable entity from one owner to another. * * @param \App\Models\User $fromOwner The current owner * @param \App\Models\User $toOwner The new owner * @param \App\Models\Post $ownable The ownable entity * @return mixed The ownable entity with transferred ownership * @throws \InvalidArgumentException when parameters are invalid */ public static function transfer(\Illuminate\Database\Eloquent\Model $fromOwner, \Illuminate\Database\Eloquent\Model $toOwner, $ownable); ``` -------------------------------- ### HasOwnables Trait Methods Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Methods available on owner models that implement the HasOwnables trait for managing ownership relationships. ```APIDOC ## HasOwnables Trait ### Description Methods available on owner models that use the HasOwnables trait. ### possessions() Get all ownership records where this model is the owner. #### Returns \Illuminate\Database\Eloquent\Relations\MorphMany ### ownables() Get all ownable entities owned by this owner. #### Returns \Illuminate\Database\Eloquent\Relations\MorphToMany ``` -------------------------------- ### JavaScript: Mobile Navigation Toggle Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Adds a functional mobile navigation toggle button to the header. This JavaScript code creates a button, appends it to the header, and adds an event listener to toggle the visibility and styling of the navigation menu for mobile devices. It requires a header and navigation element with specific classes for styling. ```javascript // Add mobile navigation toggle const nav = document.querySelector('nav'); const header = document.querySelector('header'); // Create mobile menu button const mobileMenuButton = document.createElement('button'); mobileMenuButton.className = 'md:hidden p-2 text-gray-600 hover:text-primary'; mobileMenuButton.innerHTML = ` `; // Insert mobile menu button header.querySelector('.flex').appendChild(mobileMenuButton); // Toggle mobile menu mobileMenuButton.addEventListener('click', function() { nav.classList.toggle('hidden'); nav.classList.toggle('flex'); nav.classList.toggle('flex-col'); nav.classList.toggle('absolute'); nav.classList.toggle('top-full'); nav.classList.toggle('left-0'); nav.classList.toggle('right-0'); nav.classList.toggle('bg-white'); nav.classList.toggle('shadow-lg'); nav.classList.toggle('p-4'); }); ``` -------------------------------- ### JavaScript: Smooth Scrolling for Navigation Links Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Implements smooth scrolling behavior for anchor links within the page. It prevents the default jump and smoothly scrolls the target element into view. This enhances user experience by providing a more fluid navigation. ```javascript document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const target = document.querySelector(this.getAttribute('href')); if (target) { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); ``` -------------------------------- ### Transfer Ownership To (IsOwnable Trait) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Transfers ownership of this entity to a new owner model. This method is part of the IsOwnable trait. ```php /** * Transfer ownership of this entity to a new owner. * * @param \Illuminate\Database\Eloquent\Model $newOwner The new owner model * @return $this */ public function transferOwnershipTo(\Illuminate\Database\Eloquent\Model $newOwner); ``` -------------------------------- ### Give Ownership (Owner) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Grants ownership of an ownable entity to the current owner. Returns true on success, false if the entity is already owned. ```php /** * Give ownership of an ownable entity to this owner. * * @param mixed $ownable The ownable entity * @return bool True if ownership was given successfully, false if already owned */ public function giveOwnershipTo($ownable): bool; ``` -------------------------------- ### Assign Ownership (IsOwnable Trait) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Assigns ownership of this entity to the provided owner model. Throws an exception if the model or owner is not saved. This method is part of the IsOwnable trait. ```php /** * Assign ownership of this entity to the given owner. * * @param \Illuminate\Database\Eloquent\Model $owner The owner model * @return $this * @throws \Exception when the model or owner is not saved */ public function ownedBy(\Illuminate\Database\Eloquent\Model $owner); ``` -------------------------------- ### Transfer Ownership (Owner) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Transfers ownership of an ownable entity to a new owner. Returns true if the transfer was successful, false if not owned by the current owner. ```php /** * Transfer ownership of an ownable entity to a new owner. * * @param mixed $ownable The ownable entity * @param mixed $newOwner The new owner * @return bool True if transfer was successful, false if not owned by this owner */ public function transferOwnership($ownable, $newOwner): bool; ``` -------------------------------- ### Take Ownership (Owner) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Removes ownership of an ownable entity from the current owner. Returns true if ownership was removed successfully, false if not owned. ```php /** * Remove ownership of an ownable entity from this owner. * * @param mixed $ownable The ownable entity * @return bool True if ownership was removed successfully, false if not owned */ public function takeOwnershipFrom($ownable): bool; ``` -------------------------------- ### Check Ownership (Owner Facade) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Static method to check if a specific owner owns a particular ownable entity. Returns true if owned, false otherwise. Throws InvalidArgumentException for invalid parameters. ```php /** * Check if an owner owns a specific ownable entity. * * @param \App\Models\User $owner The owner model to check * @param \App\Models\Post $ownable The ownable entity * @return bool True if the owner owns the entity, false otherwise * @throws \InvalidArgumentException when parameters are invalid */ public static function check(\Illuminate\Database\Eloquent\Model $owner, $ownable): bool; ``` -------------------------------- ### Authorize Post Ownership in Controller (PHP) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Checks if the current user owns a specific post before allowing edits or transfers. Aborts with a 403 error if the user does not own the post. ```php public function edit(Post $post) { // Check if current user owns the post if (!auth()->user()->owns($post)) { abort(403, 'You do not own this post.'); } return view('posts.edit', compact('post')); } public function transfer(Post $post, User $newOwner) { // Transfer ownership if (auth()->user()->owns($post)) { $post->transferOwnershipTo($newOwner); return redirect()->back()->with('success', 'Post transferred successfully!'); } return redirect()->back()->with('error', 'You cannot transfer this post.'); } ``` -------------------------------- ### Check Ownership (Owner) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Checks if the current owner possesses a specific ownable entity. This method is part of the owner's functionality. ```php /** * Check if this owner owns the given ownable entity. * * @param mixed $ownable The ownable entity to check * @return bool True if owns the entity, false otherwise */ public function owns($ownable): bool; ``` -------------------------------- ### Check if Owned By (IsOwnable Trait) Source: https://github.com/sowailem/ownable/blob/main/docs/index.html Checks if this entity is owned by the specified owner model. Returns true if owned, false otherwise. This method is part of the IsOwnable trait. ```php /** * Check if this entity is owned by the given owner. * * @param \Illuminate\Database\Eloquent\Model $owner The owner model to check * @return bool True if owned by the given owner, false otherwise */ public function isOwnedBy(\Illuminate\Database\Eloquent\Model $owner): bool; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.