Backend Channel - shibotsu/obs-clone GitHub Wiki

📺 Manipulating Channels in Laravel

This section describes how to work with the channels table in a Laravel backend, including creating, updating, and deleting channels. The channels table is associated with users and supports live streaming features.

Database Structure

The channels table includes the following fields:

  • id: Primary key
  • user_id: Foreign key referencing the user who owns the channel
  • stream_key: Unique stream key for broadcasting (nullable)
  • title: Channel title (nullable)
  • description: Channel description (nullable)
  • is_live: Boolean indicating if the channel is currently live (default: false)
  • stream_title: Title of the current stream (nullable)
  • stream_description: Description of the current stream (nullable)
  • stream_category: Category of the current stream (nullable)
  • created_at / updated_at: Timestamps

Creating a Channel

To create a channel, you can use Eloquent:

$channel = Channel::create([
    'user_id' => auth()->id(),
    'title' => 'My Channel',
    'description' => 'Welcome to my channel!',
    // other fields as needed
]);

This code is used in RegisteredUserConroller and when user gets created, also the channel gets created.

Updating a Channel

To update channel details:

$channel = Channel::find($id);
$channel->update([
    'title' => 'Updated Title',
    'description' => 'Updated description',
    'is_live' => true,
    // other fields as needed
]);

Deleting a Channel

They are deleted when the user gets deleted.

Example Migration

Schema::create('channels', function (Blueprint $table) {
    $table->id();
    $table->foreignIdFor(User::class)->constrained()->cascadeOnDelete();
    $table->string('stream_key')->unique()->nullable();
    $table->string('title')->nullable();
    $table->text('description')->nullable();
    $table->boolean('is_live')->default(false);
    $table->string('stream_title')->nullable();
    $table->text('stream_description')->nullable();
    $table->string('stream_category')->nullable();
    $table->timestamps();
});

Best Practices

  • Always validate input data before creating or updating channels.
  • Use policies to ensure only authorized users can manipulate their channels.
  • Use Eloquent relationships to easily access a user's channels.

For more details, see the Laravel Eloquent documentation.