2017 © Pedro Peláez
 

library eloquent-media

Associate files to your eloquent models

image

ahmed-aliraqi/eloquent-media

Associate files to your eloquent models

  • Wednesday, February 14, 2018
  • by ahmed-aliraqi
  • Repository
  • 2 Watchers
  • 4 Stars
  • 120 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 18 Versions
  • 2 % Grown

The README.md

eloquent-media

Build Status Total Downloads Latest Stable Version License , (*1)

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

Installation

composer require ahmed-aliraqi/eloquent-media

Configration

In your config/filesystems.php file confiure disks.local key to match your prefered upload path., (*3)

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

Usage

namespace App;

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

class Post extends Model
{
    use HasFiles;


    //...
}

API

putFile

Method Definition:, (*4)

/**
   * 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., (*5)

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., (*6)

NOTE: if you use primary key other than $id it will be used automatically instead of $id., (*7)

file

Method Definition:, (*8)

/**
   * 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 ., (*9)

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., (*10)

putFiles

Method Definition:, (*11)

/**
   * 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., (*12)

For example $user->putFiles('avatars'); will save the uploaded file avatars and save it in >public/uploads/users/USER_ID/avatars/583ac3d5a0135.ext where USER_ID is the user id., (*13)

files

Method Definition:, (*14)

/**
  * 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 ., (*15)

For example $user->files('avatars'); will get array collection for pathes and >links of the uploaded files avatars For example :, (*16)

@foreach($user->files('avatar') as $path => $link)
 file path : {{ $path }} <br>
 File link : {{ $link }}
@endforeach

filePath

Method Definition:, (*17)

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

hasGlobal

Method Definition:, (*18)

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

For example:, (*19)

$user->hasGlobal()->putFile('default');
  

the file will save in public/uploads/users/default.png, (*20)

{{ $user-hasGlobal()->file('default') }}
  

disk

Method Definition:, (*21)

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

For example:, (*22)

$user->disk('public')->putFile('photo');
  

blade {{ $user->disk('public')->file('photo') }}, (*23)

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 :, (*24)

<?php
return [
    // Get users fallback image url.
    'users' => 'http://lorempixel.com/grey/800/400/cats/Faker/',

    // Get posts fallback image url.
    'posts' => 'http://lorempixel.com/grey/800/400/cats/Faker/',

    // If You do not run application using artisan serve.
    'remove_public_from_url' => false,
];

The Versions

14/02 2018

dev-master

9999999-dev

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

14/02 2018

v1.2.5

1.2.5.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

13/02 2018

v1.2.4

1.2.4.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

05/02 2018

v1.2.3

1.2.3.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

05/02 2018

v1.2.2

1.2.2.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

05/02 2018

v1.2.1

1.2.1.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

22/09 2017

dev-analysis-qJxOxQ

dev-analysis-qJxOxQ

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

13/08 2017

v1.2.0

1.2.0.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

04/07 2017

v1.1.1

1.1.1.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

02/07 2017

v1.1.0

1.1.0.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

28/03 2017

v1.0.7

1.0.7.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

16/03 2017

v1.0.6

1.0.6.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

06/03 2017

v1.0.5

1.0.5.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

06/03 2017

v1.0.4

1.0.4.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

02/03 2017

v1.0.2

1.0.2.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

02/03 2017

v1.0.3

1.0.3.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

27/02 2017

v1.0.1

1.0.1.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy

27/02 2017

v1.0.0

1.0.0.0

Associate files to your eloquent models

  Sources   Download

The Requires

  • php >=5.6

 

by Ahmed Fathy