PHP Trait Plank\Mediable\Mediable

Provides functionality for attaching media to an eloquent model.
Author: Sean Fraser ([email protected])
Show file Open project: plank/laravel-mediable

Public Methods

Method Description
attachMedia ( mixed $media, string | array $tags ) : void Attach a media entity to the model with one or more tags.
bootMediable ( ) : void Boot the Mediable trait.
detachMedia ( mixed $media, string | array | null $tags = null ) : void Detach a media item from the model.
detachMediaTags ( string | array $tags ) : void Remove one or more tags from the model, detaching any media using those tags.
firstMedia ( string | array $tags, boolean $match_all = false ) : boolean Shorthand for retrieving a single attached media.
getAllMediaByTag ( ) : Collection Retrieve all media grouped by tag name.
getMedia ( string | array $tags, boolean $match_all = false ) : boolean Retrieve media attached to the model.
getMediaMatchAll ( array $tags ) : boolean Retrieve media attached to multiple tags simultaneously.
getTagsForMedia ( Media $media ) : array Get a list of all tags that the media is attached to.
hasMedia ( string | array $tags, boolean $match_all = false ) : boolean Check if the model has any media attached to one or more tags.
load ( $relations )
loadMedia ( string | array $tags = [], boolean $match_all = false ) Lazy eager load attached media relationships.
loadMediaMatchAll ( string | array $tags = [] ) Lazy eager load attached media relationships matching all provided tags.
media ( ) : Illuminate\Database\Eloquent\Relations\MorphToMany Relationship for all attached media.
newCollection ( array $models = [] ) : Plank\Mediable\MediableCollection
scopeWhereHasMedia ( Builder $q, string | array $tags, boolean $match_all = false ) : void Query scope to detect the presence of one or more attached media for a given tag.
scopeWhereHasMediaMatchAll ( Builder $q, array $tags ) : void Query scope to detect the presence of one or more attached media that is bound to all of the specified tags simultaneously.
scopeWithMedia ( Builder $q, string | array $tags = [], boolean $match_all = false ) : void Query scope to eager load attached media.
scopeWithMediaMatchAll ( Builder $q, string | array $tags = [] ) : void Query scope to eager load attached media assigned to multiple tags.
syncMedia ( mixed $media, string | array $tags ) : void Replace the existing media collection for the specified tag(s).

Protected Methods

Method Description
addMatchAllToEagerLoadQuery ( Illuminate\Database\Eloquent\Relations\MorphToMany $q, array | string $tags = [] ) : void Modify an eager load query to only load media assigned to all provided tags simultaneously.
handleMediableDeletion ( ) : void Determine whether media relationships should be detached when the model is deleted or soft deleted.
markMediaDirty ( string | array $tags ) : void Indicate that the media attached to the provided tags has been modified.
mediaIsDirty ( null | string | array $tags = null ) : boolean Check if media attached to the specified tags has been modified.
newMatchAllQuery ( array | string $tags = [] ) : [type] Generate a query builder for.
rehydrateMediaIfNecessary ( null | string | array $tags = null ) : void Reloads media relationship if allowed and necessary.
rehydratesMedia ( ) : boolean Check whether the model is allowed to automatically reload media relationship.

Private Methods

Method Description
extractIds ( mixed $input ) : array Convert mixed input to array of ids.
getOrderValueForTags ( string | array $tags ) : integer Determine the highest order value assigned to each provided tag.

Method Details

addMatchAllToEagerLoadQuery() protected method

Modify an eager load query to only load media assigned to all provided tags simultaneously.
protected addMatchAllToEagerLoadQuery ( Illuminate\Database\Eloquent\Relations\MorphToMany $q, array | string $tags = [] ) : void
$q Illuminate\Database\Eloquent\Relations\MorphToMany
$tags array | string
return void

attachMedia() public method

Attach a media entity to the model with one or more tags.
public attachMedia ( mixed $media, string | array $tags ) : void
$media mixed Either a string or numeric id, an array of ids, an instance of `Media` or an instance of `\Illuminate\Database\Eloquent\Collection`
$tags string | array One or more tags to define the relation
return void

bootMediable() public static method

Boot the Mediable trait.
public static bootMediable ( ) : void
return void

detachMedia() public method

Detach a media item from the model.
public detachMedia ( mixed $media, string | array | null $tags = null ) : void
$media mixed
$tags string | array | null If provided, will remove the media from the model for the provided tag(s) only If omitted, will remove the media from the media for all tags
return void

detachMediaTags() public method

Remove one or more tags from the model, detaching any media using those tags.
public detachMediaTags ( string | array $tags ) : void
$tags string | array
return void

firstMedia() public method

Shorthand for retrieving a single attached media.
See also: Plank\Mediable\Mediable::getMedia()
public firstMedia ( string | array $tags, boolean $match_all = false ) : boolean
$tags string | array
$match_all boolean
return boolean

getAllMediaByTag() public method

Retrieve all media grouped by tag name.
public getAllMediaByTag ( ) : Collection
return Illuminate\Support\Collection

getMedia() public method

Retrieve media attached to the model.
public getMedia ( string | array $tags, boolean $match_all = false ) : boolean
$tags string | array
$match_all boolean If false, will return media attached to any of the provided tags If true, will return media attached to all of the provided tags simultaneously
return boolean

getMediaMatchAll() public method

Retrieve media attached to multiple tags simultaneously.
public getMediaMatchAll ( array $tags ) : boolean
$tags array
return boolean

getTagsForMedia() public method

Get a list of all tags that the media is attached to.
public getTagsForMedia ( Media $media ) : array
$media Media
return array

handleMediableDeletion() protected method

Determine whether media relationships should be detached when the model is deleted or soft deleted.
protected handleMediableDeletion ( ) : void
return void

hasMedia() public method

Check if the model has any media attached to one or more tags.
public hasMedia ( string | array $tags, boolean $match_all = false ) : boolean
$tags string | array
$match_all boolean If false, will return true if the model has any attach media for any of the provided tags If true, will return true is the model has any media that are attached to all of provided tags simultaneously
return boolean

load() public method

public load ( $relations )

loadMedia() public method

Lazy eager load attached media relationships.
public loadMedia ( string | array $tags = [], boolean $match_all = false )
$tags string | array If one or more tags are specified, only media attached to those tags will be loaded.
$match_all boolean Only load media matching all provided tags

loadMediaMatchAll() public method

Lazy eager load attached media relationships matching all provided tags.
public loadMediaMatchAll ( string | array $tags = [] )
$tags string | array one or more tags

markMediaDirty() protected method

Indicate that the media attached to the provided tags has been modified.
protected markMediaDirty ( string | array $tags ) : void
$tags string | array
return void

media() public method

Relationship for all attached media.
public media ( ) : Illuminate\Database\Eloquent\Relations\MorphToMany
return Illuminate\Database\Eloquent\Relations\MorphToMany

mediaIsDirty() protected method

Check if media attached to the specified tags has been modified.
protected mediaIsDirty ( null | string | array $tags = null ) : boolean
$tags null | string | array If omitted, will return `true` if any tags have been modified
return boolean

newCollection() public method

public newCollection ( array $models = [] ) : Plank\Mediable\MediableCollection
$models array
return Plank\Mediable\MediableCollection

newMatchAllQuery() protected method

Generate a query builder for.
protected newMatchAllQuery ( array | string $tags = [] ) : [type]
$tags array | string [description]
return [type]

rehydrateMediaIfNecessary() protected method

Reloads media relationship if allowed and necessary.
protected rehydrateMediaIfNecessary ( null | string | array $tags = null ) : void
$tags null | string | array
return void

rehydratesMedia() protected method

Can be overridden by setting protected property $rehydrates_media on the model.
protected rehydratesMedia ( ) : boolean
return boolean

scopeWhereHasMedia() public method

Query scope to detect the presence of one or more attached media for a given tag.
public scopeWhereHasMedia ( Builder $q, string | array $tags, boolean $match_all = false ) : void
$q Illuminate\Database\Eloquent\Builder
$tags string | array
$match_all boolean
return void

scopeWhereHasMediaMatchAll() public method

Query scope to detect the presence of one or more attached media that is bound to all of the specified tags simultaneously.
public scopeWhereHasMediaMatchAll ( Builder $q, array $tags ) : void
$q Illuminate\Database\Eloquent\Builder
$tags array
return void

scopeWithMedia() public method

Query scope to eager load attached media.
public scopeWithMedia ( Builder $q, string | array $tags = [], boolean $match_all = false ) : void
$q Illuminate\Database\Eloquent\Builder
$tags string | array If one or more tags are specified, only media attached to those tags will be loaded.
$match_all boolean Only load media matching all provided tags
return void

scopeWithMediaMatchAll() public method

Query scope to eager load attached media assigned to multiple tags.
public scopeWithMediaMatchAll ( Builder $q, string | array $tags = [] ) : void
$q Illuminate\Database\Eloquent\Builder
$tags string | array
return void

syncMedia() public method

Replace the existing media collection for the specified tag(s).
public syncMedia ( mixed $media, string | array $tags ) : void
$media mixed
$tags string | array
return void