PHP code example of robotsinside / laravel-categories

1. Go to this page and download the library: Download robotsinside/laravel-categories 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/ */

    

robotsinside / laravel-categories example snippets


/*
* Package Service Providers...
*/
\RobotsInside\Categories\CategoriesServiceProvider::class,



namespace App;

use Illuminate\Database\Eloquent\Model;
use RobotsInside\Categories\Categorisable;

class Post extends Model
{
    use Categorisable;
}



use App\Post;
use Illuminate\Support\Facades\Route;
use RobotsInside\Categories\Models\Category;

Route::get('/', function () {

    // Retrieve a new or existing category
    $category1 = (new Category())->resolve('Category 1');
    $category2 = (new Category())->resolve('Category 2');

    // Or, retrieve a collection of new or existing categories
    $categories = (new Category())->resolveAll(['Category 1', 'Category 2', 'Category 3'])

    $post = new Post();
    $post->title = 'My blog';
    $post->save();

    $post->categorise($category1);
    // Or
    $post->categorise(['category-1']);
    // Or
    $post->categorise([1, 2]);
    // Or
    $post->categorise(Category::get());
});



use App\Post;
use Illuminate\Support\Facades\Route;
use RobotsInside\Categories\Models\Category;

Route::get('/', function () {

    $category1 = Category::find(1);

    $post = Post::where('title', 'My blog')->first();

    $post->uncategorise($category1);
    // Or
    $post->uncategorise(['category-1']);
    // Or
    $post->uncategorise([1, 2]);
    // Or
    $post->uncategorise(Category::get());
    // Or
    $post->uncategorise(); // remove all categories
});
sh
php artisan vendor:publish --provider="RobotsInside\Categories\CategoriesServiceProvider" --tag="migrations"
sh
php artisan migrate