Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created January 14, 2024 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbean/ab17743576fb05d9444ea3de17de1df9 to your computer and use it in GitHub Desktop.
Save martinbean/ab17743576fb05d9444ea3de17de1df9 to your computer and use it in GitHub Desktop.
Shareable
<?php
use App\Http\Controllers\ShareableController;
use Illuminate\Support\Facades\Route;
// Shareable Routes...
Route::get('/share/{shareable:source_ulid}', [ShareableController::class, 'show']);
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class Shareable extends Model
{
public function source(): MorphTo
{
return $this->morphTo();
}
}
<?php
namespace App\Http\Controllers;
use App\Models\Shareable;
use Illuminate\Routing\Controller;
class ShareableController extends Controller
{
public function show(Shareable $shareable)
{
// Access underlying model with $shareable->source
// Redirect or whatever when you know the source model
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment