PHP code example of veiliglanceren / laravel-morph-categories

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

    

veiliglanceren / laravel-morph-categories example snippets


use VeiligLanceren\LaravelMorphCategories\Traits\HasCategory;

class Post extends Model
{
    use HasCategory;
}

$post = Post::find(1);
$category = MorphCategory::create(['name' => 'News']);

$post->attachCategory($category);
$post->detachCategory($category);
$post->syncCategories([$category->id]);

$post->hasCategory('news'); // by slug
$post->hasCategory($category); // by model
$post->hasCategory($category->id); // by ID

return [

    'tables' => [
        'categories' => 'categories',
        'categoryables' => 'categoryables',
    ],

    'models' => [
        'category' => \VeiligLanceren\LaravelMorphCategories\Models\MorphCategory::class,
        'categoryable' => \VeiligLanceren\LaravelMorphCategories\Models\MorphCategoryable::class,
    ],
];

$post->morphCategories; // Collection of MorphCategory models

$category->morphCategoryables; // MorphToMany to all related models

Post::withCategory('news')->get();
Post::withCategory($category)->get();
bash
php artisan vendor:publish --tag=category-config