ash-jc-allen / short-url

A Laravel package for creating shortened URLs for your web apps.
MIT License
1.25k stars 158 forks source link

Custom Tables and Models #253

Closed c-delouvencourt closed 4 months ago

c-delouvencourt commented 4 months ago

Custom Tables and Models

By default, the package will use the short_urls and short_url_visits tables and the ShortURL and ShortURLVisit models. However, you may want to use your own custom tables and models.

To do this, you can set the connection name using the urls_model, urls_table, visits_model and visits_table' config value in the config/short-url.php file like so:

'urls_model' => \AshAllenDesign\ShortURL\Models\ShortURL::class,
'urls_table' => 'short_urls',

'visits_model' => \AshAllenDesign\ShortURL\Models\ShortURLVisit::class,
'visits_table' => 'short_url_visits',

And then create your own models that extend the package's models:

namespace App\Models;

use AshAllenDesign\ShortURL\Models\ShortURL as ShortURLModel;

class ShortURL extends ShortURLModel
{
    // Your custom code here.
}
namespace App\Models;

use AshAllenDesign\ShortURL\Models\ShortURLVisit as ShortURLVisitModel;

class ShortURLVisit extends ShortURLVisitModel
{
    // Your custom code here.
}
ash-jc-allen commented 4 months ago

Hey @c-delouvencourt, thanks for the PR, it looks well thought out! And huge sorry for taking so long to get back to you on this one. The last few weeks have been hectic for me, so I'm just trying to catch up now.

I do think this is a pretty neat idea that could add some extra customisation. But I think I'm going to hold off on merging it for the time being since it adds extra code/logic for me to maintain. However, if enough devs think this would be helpful to them, I'll definitely think about implementing this! 😄

chrisreedio commented 3 months ago

For what it's worth, I was just trying to override the ShortURL model to add a reverse relationship (morph) and discovered that was no mechanism to do so.

Searching the issues for 'model' led me here.

I would find this functionality useful.