Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

app/Models/Group.php

Open in GitHub
use Illuminate\Database\Eloquent\Relations\MorphToMany;
 
class Group extends Model
{
//
public function links(): MorphToMany
{
return $this->morphedByMany(Link::class, 'groupable');
}
//
}

app/Models/Link.php

Open in GitHub
use Illuminate\Database\Eloquent\Relations\MorphToMany;
 
class Link extends Model
{
//
public function groups(): MorphToMany
{
return $this->morphToMany(Group::class, 'groupable');
}
//
}

app/Http/Controllers/LinkController.php

Open in GitHub
class LinkController extends Controller
{
//
public function store(StoreLinkRequest $request): RedirectResponse
{
//
 
$groupIds = $validated['groups'];
 
$link->groups()->sync($groupIds);
 
//
 
return Redirect::route('links.show', $link->id);
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.