Download the PHP package esign/laravel-linkable without Composer
On this page you can find all versions of the php package esign/laravel-linkable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download esign/laravel-linkable
More information about esign/laravel-linkable
Files in esign/laravel-linkable
Package laravel-linkable
Short Description Dynamically link Laravel models
License MIT
Homepage https://github.com/esign/laravel-linkable
Informations about the package laravel-linkable
Dynamically link Laravel models
This package allows you to add a dynamic link to a Laravel model. This dynamic link may be a reference to another model or could also be an external URL. In essence, this package is just a Laravel relationship with some extra utility methods.
Installation
You can install the package via composer:
Usage
Preparing your model
To make a model have a dynamic link you may add the HasDynamicLink
trait.
Your database structure should look like the following:
Internal links
In case you set the dynamic_link_type
as internal
the dynamic_link_linkable_model
field will be used.
In order to know where a internal dynamic link should direct to you may implement LinkableUrlContract
on the related model:
External links
In case you set the dynamic_link_type
as external
the dynamic_link_url
field will be used.
The value of this field should be a valid URL, e.g. https://www.example.com
.
Storing linkables
Instead of using a regular MorphTo
relation, this package ships with a SingleColumnMorphTo
relation.
Some CMS, including our own, do not allow for morphable relations based on two columns, e.g. dynamic_link_linkable_type
and dynamic_link_linkable_id
.
The SingleColumnMorphTo
combines both the type and id fields into a single column, e.g. dynamic_link_linkable_model
.
The value for this single column is stored in the {model}:{id}
format, e.g. post:1
.
You're able to use both a fully qualified class name or a value from your application's morph map, just like a regular morphTo relation.
Note This approach is not ideal and more complex queries using this relationship may not work as expected. In case you're able to you may overwrite the
dynamicLinkLinkable
relation to use Laravel's defaultMorphTo
relationship.
Linkables overview
To create an overview of all possible linkables you can create a MySQL view that creates a union of all possible models that can be linked to:
This would create the following output: | id | linkable_type | linkable_id | label |
---|---|---|---|---|
post:1 | post | 1 | My First Post | |
comment:1 | comment | 1 | My First Comment |
Rendering dynamic links
This package ships with a view component that will help you render both internal and external links:
The view component will render an <a>
tag, when a model of the type external
is given, the target="_blank"
and rel="noopener"
attributes will be applied.
Extending this package
Creating your own dynamic link types
This Laravel package offers built-in support for external links by default. However, in certain scenarios, you might want to introduce custom link types, such as referencing an anchor tag. To achieve this, you can extend the package's functionality.
-
Extend the
HasDynamicLink
Trait - Create Your Own View Component
Next, you need to create your own view component that extends the DynamicLink
component provided by the package.
Here's how you can do that:
- Use Your Custom View Component
With your custom view component in place, you can now use it in your Blade templates instead of the one provided by the package:
Testing
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-linkable with dependencies
illuminate/database Version ^8.0|^9.0|^10.0|^11.0
illuminate/support Version ^8.0|^9.0|^10.0|^11.0
illuminate/view Version ^8.0|^9.0|^10.0|^11.0