Giter VIP home page Giter VIP logo

laravel-categorizable's Introduction

Laravel Categorizable Package

This Package enables you to Categorize your Eloquent Models. just use the trait in the model and you're good to go.

Requirements

PHP 7.2+ Laravel 5.8+

Composer Install

composer require alibayat/laravel-categorizable

Publish and Run the migrations

php artisan vendor:publish --provider="AliBayat\LaravelCategorizable\CategorizableServiceProvider"

php artisan migrate

Laravel Categorizable package will be auto-discovered by Laravel. and if not: register the package in config/app.php providers array manually.

'providers' => [
	...
	\AliBayat\LaravelCategorizable\CategorizableServiceProvider::class,
],

Setup models - just use the Trait in the Model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use AliBayat\LaravelCategorizable\Categorizable;

class Post extends Model
{
	use Categorizable;

}

Usage

first of all we need to create some Category to work with. Laravel Categorizable package relies on another package called laravel-nestedset that is responsible for creating, updating, removing and retrieving single or nested categories. Here i demonstrate how to create categories and assign one as the other's child.. but you can always refer to package's repository for full documentation. https://github.com/lazychaser/laravel-nestedset

use App\Post;
use AliBayat\LaravelCategorizable\Category;

// first we create a bunch of categories

// create "BackEnd" category
Category::create([
	'name' => 'BackEnd'
]);

// create "PHP" category
Category::create([
	'name' => 'PHP'
]);

// create "FrontEnd" category
Category::create([
	'name' => 'FrontEnd'
]);

// create "Test" Category (alternative way)
$test = new Category();
$test->name = 'Test';
$test->save();


// assign "PHP" as a child of "BackEnd" category
$parent = Category::findByName('BackEnd');
$child = Category::findByName('PHP');
$parent->appendNode($child);

// delete "Test" Category
$testObj = Category::findByName('Test');
$testObj->delete();



//  assuming that we have these variables
$post = Post::first();

// 3 different ways of getting a category's instance
$backendCategory = Category::findById(1);	// 'BackEnd'
$phpCategory = Category::findByName('PHP');	// 'PHP'
$frontendCategory = Category::find(3);		// 'FrontEnd'

Attach the post to category

    $post->attachCategory($phpCategory);

Detach the post from a category

    $post->detachCategory($phpCategory); 

Attach the post to list of categories

    $post->syncCategories([
	    $phpCategory,
	    $backendCategory
	    ]); 

Detach the post from all categories

    $post->syncCategories([]); 

Sync the categories attached to a post

    $post->syncCategories([
	    $frontendCategory
	    ]); 


    // removes attached categories & adds the given categories

Check if post is attached to categories (boolean)

    // single use case
    $post->hasCategory($phpCategory);

    // multiple use case
    $post->hasCategory([
	    $phpCategory,
	    $backendCategory
	    ]);


    // return boolean

List of categories attached to the post (array)

    $post->categoriesList();


    // return array [id => name]

Get all posts attached to given category (collection)

    $categoryPosts = Category::find(1)
	    ->entries(Post::class)
	    ->get();


    // return collection

categories() Relationship

    $postWithCategories = Post::with('categories')
	    ->get();


     // you have access to categories() relationship in case you need eager loading
    

Credits

laravel-categorizable's People

Contributors

alibayat 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.