PHP code example of esign / laravel-linkable

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

    

esign / laravel-linkable example snippets


use Esign\Linkable\Concerns\HasDynamicLink;
use Illuminate\Database\Eloquent\Model;

class MenuItem extends Model
{
    use HasDynamicLink;
}

Schema::create('menu_items', function (Blueprint $table) {
    $table->id();
    $table->string('dynamic_link_type')->nullable();
    $table->string('dynamic_link_url')->nullable();
    $table->string('dynamic_link_linkable_model')->nullable();
});

use Esign\Linkable\Contracts\LinkableUrlContract;

class Post extends Model implements LinkableUrlContract
{
    public function linkableUrl(): ?string
    {
        return "http://localhost/posts/{$this->id}";
    }
}

DB::statement('
    CREATE OR REPLACE VIEW linkables AS
    SELECT
        CONCAT("post:", id) AS id,
        "post" AS linkable_type,
        id AS linkable_id,
        CONCAT("Post - ", title) AS label
    FROM posts
    UNION
    SELECT
        CONCAT("comment:", id) AS id,
        "comment" AS linkable_type,
        id AS linkable_id,
        CONCAT("Comment - ", title) AS label
    FROM comments
');

use Esign\Linkable\Concerns\HasDynamicLink;
use App\Models\Post;
use App\Models\MenuItem;

$post = Post::create();
$menuItemInternal = MenuItem::create([
    'dynamic_link_type' => HasDynamicLink::$linkTypeInternal,
    'dynamic_link_url' => null,
    'dynamic_link_linkable_model' => "post:{$post->id}",
]);

$menuItemExternal = MenuItem::create([
    'dynamic_link_type' => HasDynamicLink::$linkTypeExternal,
    'dynamic_link_url' => 'https://www.esign.eu',
    'dynamic_link_linkable_model' => null,
]);

namespace App\Models\Concerns;

use Esign\Linkable\Concerns\HasDynamicLink as BaseHasDynamicLink;

trait HasDynamicLink
{
    use BaseHasDynamicLink {
        dynamicLink as baseDynamicLink;
    }

    public static string $linkTypeAnchor = 'anchor';

    public function dynamicLink(): ?string
    {
        return match ($this->dynamicLinkType()) {
            static::$linkTypeAnchor => $this->dynamicLinkUrl(),
            default => $this->baseDynamicLink(),
        };
    }
}

namespace App\View\Components;

use App\Models\Concerns\HasDynamicLink;
use Esign\Linkable\View\Components\DynamicLink as BaseDynamicLink;

class DynamicLink extends BaseDynamicLink
{
    public function render(): ?View
    {
        return match ($this->model->dynamicLinkType()) {
            HasDynamicLink::$linkTypeAnchor => view('linkable.dynamic-link-anchor'),
            default => parent::render(),
        };
    }
}