PHPackages                             ahmed-aliraqi/eloquent-media - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. ahmed-aliraqi/eloquent-media

ActiveLibrary[File &amp; Storage](/categories/file-storage)

ahmed-aliraqi/eloquent-media
============================

Associate files to your eloquent models

v1.2.5(8y ago)41432PHPPHP &gt;=5.6

Since Feb 27Pushed 8y ago2 watchersCompare

[ Source](https://github.com/ahmed-aliraqi/eloquent-media)[ Packagist](https://packagist.org/packages/ahmed-aliraqi/eloquent-media)[ RSS](/packages/ahmed-aliraqi-eloquent-media/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (5)DependenciesVersions (18)Used By (0)

eloquent-media
==============

[](#eloquent-media)

[![Build Status](https://camo.githubusercontent.com/226a5f09bc713624f944fe438c1f8a098981a7ed6e56e1574957185f5c1b534c/68747470733a2f2f7472617669732d63692e6f72672f61686d65642d616c69726171692f656c6f7175656e742d6d656469612e737667)](https://travis-ci.org/ahmed-aliraqi/eloquent-media)[![Total Downloads](https://camo.githubusercontent.com/0fe980e047747a78ef03e6f01776ad3a1468d5956f4078148edf5d20a82908cc/68747470733a2f2f706f7365722e707567782e6f72672f61686d65642d616c69726171692f656c6f7175656e742d6d656469612f642f746f74616c2e737667)](https://packagist.org/packages/ahmed-aliraqi/eloquent-media)[![Latest Stable Version](https://camo.githubusercontent.com/3e0df0674d962989b1f8c7b32b8a79570f8b116ab927973fe4fe53f2a747f7ff/68747470733a2f2f706f7365722e707567782e6f72672f61686d65642d616c69726171692f656c6f7175656e742d6d656469612f762f737461626c652e737667)](https://packagist.org/packages/ahmed-aliraqi/eloquent-media)[![License](https://camo.githubusercontent.com/56c4437fd813ee20e59a4387eedc218fba7774e9932d5443149fd7670c08c19e/68747470733a2f2f706f7365722e707567782e6f72672f61686d65642d616c69726171692f656c6f7175656e742d6d656469612f6c6963656e73652e737667)](https://packagist.org/packages/ahmed-aliraqi/eloquent-media)

A php trait that's provides an easy way to associate images and/or files for your laravel eloquent models.

### Installation

[](#installation)

```
composer require ahmed-aliraqi/eloquent-media

```

### Configration

[](#configration)

In your `config/filesystems.php` file confiure `disks.local` key to match your prefered upload path.

```
'local' => [
    'driver' => 'local',
    'root' => public_path('uploads'),
],
```

### Usage

[](#usage)

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use Aliraqi\Traits\HasFiles;

class Post extends Model
{
    use HasFiles;

    //...
}
```

API
---

[](#api)

#### putFile

[](#putfile)

Method Definition:

> ```
> /**
>  * Upload given file to this model instance.
>  *
>  * @param  string  $key
>  * @param  string  $name
>  * @param  array   $options
>  * @return string  File path
>  */
> $model->putFile($key, [$name, $options]);
> ```
>
>
>
> NOTE: the uploaded file will be saved in the configured path in `filesystems.php file`.

> For example `$user->putFile('avatar');` will save the uploaded file `avatar` and save it in `public/uploads/users/USER_ID/avatar.EXT` where USER\_ID is the user id and EXT is the uploaded file extension.

> NOTE: if you use primary key other than $id it will be used automatically instead of $id.

#### file

[](#file)

Method Definition:

> ```
> /**
>  * Get link of given file name that belongs to this model instance.
>  *
>  * @param  string $name
>  * @param  string $fallback
>  * @return string
>  */
> $model->file([$name, $fallback]);
> ```
>
>
>
> NOTE: get the link of uploaded file .
>
> For example `$user->file('avatar');` will get link of the uploaded file `avatar``http://localhost:8000/uploads/users/USER_ID/avatar.ext` where USER\_ID is the user id.

#### putFiles

[](#putfiles)

Method Definition:

> ```
> /**
>  * Upload given files to this model instance.
>  *
>  * @param  string  $key
>  * @param  string  $name
>  * @param  boolean  $delete
>  * @param  array  $options
>  * @return string  File path
>  */
> $model->putFiles($key, [$name, $delete, $options]);
> ```
>
>
>
> NOTE: the multiple uploaded files will be saved in the configured path in `filesystems.php file`.
>
> For example `$user->putFiles('avatars');` will save the uploaded file `avatars` and save it in &gt;`public/uploads/users/USER_ID/avatars/583ac3d5a0135.ext` where USER\_ID is the user id.

#### files

[](#files)

Method Definition:

> ```
> /**
> * Get array of given files name that belongs to this model instance.
> *
> * @param  string $name  name of folder
> * @return Illuminate\Support\Collection
> */
> public function files($name)
> ```
>
>
>
> NOTE: get the link of uploaded file .
>
> For example `$user->files('avatars');` will get array collection for pathes and &gt;links of the uploaded files `avatars`For example :
>
> ```
> @foreach($user->files('avatar') as $path => $link)
>  file path : {{ $path }}
>  File link : {{ $link }}
> @endforeach
>
> ```

#### filePath

[](#filepath)

Method Definition:

> ```
> /**
>  * Get path of given file name that belongs to this model instance.
>  *
>  * @param  string $name
>  * @return string | null
>  */
> public function filePath([$name])
> ```

#### hasGlobal

[](#hasglobal)

Method Definition:

> ```
> /**
>  * Determine if the assiciated files is global or not.
>  *
>  * @param  boolean $value
>  * @return Illuminate\Database\Eloquent\Model
>  */
> public function hasGlobal([$value = true])
> ```
>
>
>
> For example:

> ```
> $user->hasGlobal()->putFile('default');
> ```
>
>
>
> the file will save in `public/uploads/users/default.png`
>
> ```
> {{ $user-hasGlobal()->file('default') }}
> ```

#### disk

[](#disk)

Method Definition:

> ```
> /**
>  * Determine a filesystem instance.
>  *
>  * @param  string  $name
>  * @return Illuminate\Database\Eloquent\Model
>  */
> public function disk([$name = 'local'])
> ```
>
>
>
> For example:

> ```
> $user->disk('public')->putFile('photo');
> ```
>
>
>
> ```
> {{ $user->disk('public')->file('photo') }}
> ```

### Fallback images.

[](#fallback-images)

If you want to return fallback image if given image not found you must create `config/fallbackimages.php` file and put the name of model table and set fallback image like this :

```
