Skip to main content
Channels control where products and other models are published across different storefronts.

Overview

Channels allow products and other models to be published to different storefronts or sales channels. Lunar installs a default webstore channel during setup.
Lunar\Models\Channel

Fields

FieldTypeDescription
ididPrimary key
namestringThe display name of the channel
handlestringURL-friendly identifier, automatically slugified on save
defaultbooleanWhether this is the default channel
urlstring nullableAn optional URL associated with the channel
created_attimestamp
updated_attimestamp
deleted_attimestamp nullable

Relationships

RelationshipTypeRelated ModelDescription
productsMorphToManyLunar\Models\ProductProducts assigned to this channel
collectionsMorphToManyLunar\Models\CollectionCollections assigned to this channel
discountsMorphToManyLunar\Models\DiscountDiscounts assigned to this channel

Scopes

ScopeDescription
default($default = true)Filter to the default channel

Assigning channels to models

Models can be assigned to different channels and optionally scheduled for availability within a date range. To add this functionality to a model, use the HasChannels trait:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Lunar\Base\Traits\HasChannels;

class Product extends Model
{
    use HasChannels;
}
When using this trait, the scheduleChannel method becomes available:
use Lunar\Models\Channel;

$channel = Channel::first();

// Schedule the product to be enabled in 14 days and disabled after 24 days.
$product->scheduleChannel($channel, now()->addDays(14), now()->addDays(24));

// Enable the product on the channel immediately.
$product->scheduleChannel($channel);

// The method also accepts a collection of channels.
$product->scheduleChannel(Channel::get());
There is also a channel scope available to models using this trait:
use Lunar\Models\Product;

// Limit to a single channel
Product::channel($channel)->get();

// Limit to multiple channels
Product::channel([$channelA, $channelB])->get();

// Limit to a channel available the next day
Product::channel($channelA, now()->addDay())->get();

// Limit to a channel within a date range
Product::channel($channelA, now()->addDay(), now()->addDays(2))->get();