Skip to content

HasSanchayaFiles Trait ​

Add the HasSanchayaFiles trait to any Eloquent model to enable polymorphic, grouped media attachments without altering your table schemas.

php
use DP0\Sanchaya\Traits\HasSanchayaFiles;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasSanchayaFiles;
}

Reading Attachments

php
// Get Eloquent Collection of SanchayaFile in 'gallery' group
$gallery = $post->sanchayaFiles('gallery');

// Get first file in 'hero' group
$heroFile = $post->sanchayaFile('hero');

// Get public or temporary URL directly
$heroUrl = $post->sanchayaUrl('hero');

// Get array of file IDs
$ids = $post->sanchayaFileIds('gallery');

// Check if any files are attached
$hasAvatar = $post->hasSanchayaFiles('avatar');

Writing Attachments

php
// Attach single file
$post->attachSanchayaFile($fileId, 'avatar');

// Sync a group to an exact ordered list of IDs (removes missing, adds new)
$post->syncSanchayaFiles([12, 14, 19], 'gallery');

// Detach all files in a group
$post->detachSanchayaFiles('gallery');

// Detach a specific file
$post->detachSanchayaFile($fileId, 'gallery');

Released under the MIT License.