Usage

Single Column Single Image

For example Category Model

  • you need to add a nullable json column for your model

php artisan make:migration add_image_to_categories_table
  • the migration file:

      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::table('categories', function (Blueprint $table) {
              $table->json('image')->nullable();
          });
      }
  • Inside the model class:

<?php

namespace App;

use HusseinFeras\Laraimage\Traits\SingleColumnSingleImage;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use SingleColumnSingleImage;

    /*
     * this is the name of your image column in the database
    */
    public $imageColumn = 'image';

    /*
     * add your image column in the casts array
    */
    protected $casts = [
        'image' => 'json'
    ];

    /*
     * the storage path where the images will stored
    */
    public function imagesPath()
    {
        return 'categories/images/'.$this->id;
    }
}

This Trait SingleColumnSingleImage adds these functions that used in controller methods:

    /*
     * add new image using the request key
     *
     * @param  string  $requestKey
    */
    public function addImage(string $requestKey) : void {}

    /*
     * delete the image
     *
     * @return  boolean
    */
    public function deleteImage(): bool {}
    /*
     * get the image url
     *
     * @return  string image url | default image
    */
    public function getImage() {}

Note: when you delete the model using delete() method this trait will listen to the deleting event and delete the image files automatically

Single Column Multi Image

For example Products Model

  • you need to add a nullable json column for your model

php artisan make:migration add_image_to_products_table
  • the migration file:

      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::table('products', function (Blueprint $table) {
              $table->json('images')->nullable();
          });
      }
  • Inside the model class:

<?php

namespace App;

use HusseinFeras\Laraimage\Traits\SingleColumnMultiImage;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use SingleColumnMultiImage;
    /*
     * this is the name of your images column in the database
    */
    public $imagesColumn = 'images';
       /*
        * add your image column in the casts array
       */
       protected $casts = [
           'images' => 'json'
       ];

       /*
        * the storage path where the images will stored
       */
       public function imagesPath()
       {
           return 'products/images/'.$this->id;
       }
}

This Trait SingleColumnMultiImage adds these functions that used in controller methods:

      /*
       * add new images using the request key
       * the $append flag decide with you want to overwrite the existing images in the database or append to them
       *
       * @param  string  $requestKey
       * @param  bool  $append
      */
      public function addImages($requestKey,$append = false) : void {}
      /*
        * delete images
        * if $id is null this function will delete all the images in the images column
        * if $id is the value of id of an image then this image only will delete
        *
        * @param  integer  $id
        * @return  boolean
       */
       public function deleteImages($id = null): bool {}
       /*
        * get the images urls
        *
        * @return  array  images url | default image
       */
       public function getImages(): array {}

Note: when you delete the model using delete() method this trait will listen to the deleting event and delete the images files automatically

Multi Column Single Image

For example Post Model

  • you need to add two or more nullable json columns for your model

php artisan make:migration add_image_to_posts_table
  • the migration file:

      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::table('posts', function (Blueprint $table) {
              $table->json('poster')->nullable();
              $table->json('cover')->nullable();
          });
      }
  • Inside the model class:

<?php

namespace App;

use HusseinFeras\Laraimage\Traits\MultiColumnSingleImage;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use MultiColumnSingleImage;
    /*
     * this is the name of your image's columns in the database
    */
    protected $imageColumns = ['poster','cover'];
       /*
        * add your image column in the casts array
       */
       protected $casts = [
        'poster' => 'json',
        'cover' =>  'json',
       ];

       /*
        * the storage path where the images will stored
       */
       public function imagesPath()
       {
           return 'posts/images/'.$this->id;
       }
}

This Trait MultiColumnSingleImage adds these functions that used in controller methods:

      /*
       * add new image using the request key and specifying the column
       *
       * @param  string  $requestKey
       * @param  string  $imageColumn
      */
      public function addImage($imageColumn,$requestKey): void {}

       /*
       * delete the image by specifying the column
       *
       * @param  string  $imageColumn
       * @return  boolean
      */
       public function deleteImage($imageColumn): bool {}

      /*
       * delete all images in all image columns
       *
      */
      public function deleteAllImages(): void {}

      /*
       * get the image url by specifying the column
       *
       * @param  string  $imageColumn
       * @return  string image url | default image
      */
      public function getImage($imageColumn) {}

Note: when you delete the model using delete() method this trait will listen to the deleting event and delete the images files automatically

Multi Column Multi Image

For example Post Model

  • you need to add two or more nullable json columns for your model

php artisan make:migration add_images_to_posts_table
  • the migration file:

      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::table('posts', function (Blueprint $table) {
              $table->json('slider')->nullable();
              $table->json('images')->nullable();
          });
      }
  • Inside the model class:

<?php

namespace App;

use HusseinFeras\Laraimage\Traits\MultiColumnMultiImage;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use MultiColumnMultiImage;
    /*
     * this is the name of your image's columns in the database
    */
    protected $imagesColumns = ['slider','images'];
       /*
        * add your image column in the casts array
       */
       protected $casts = [
        'slider' => 'json',
        'images' =>  'json',
       ];

       /*
        * the storage path where the images will stored
       */
       public function imagesPath()
       {
           return 'posts/images/'.$this->id;
       }
}

This Trait MultiColumnMultiImage adds these functions that used in controller methods:

    /*
      * add new image using the request key and specifying the column
     * the $append flag decide with you want to overwrite the existing images in the database or append to them
     *
     * @param  string  $requestKey
     * @param  string  $imageColumn
     * @param  bool  $append
     */
    public function addImages($imagesColumn,$requestKey,$append = false) {}

     /*
      * delete images by specifying the column
      * if $id is null this function will delete all the images in the images column
      * if $id is the value of id of an image then this image only will delete
      *
      * @param  integer  $id
      * @return  boolean
     */
     public function deleteImages($imagesColumn,$id = null): bool {}

      /*
      * delete all images in all image columns
      *
     */
     public function deleteAllImages(): void {}

     /*
      * get the images urls
      *
      * @return  array  images url | default image
     */
     public function getImages($imagesColumn): array {}

Note: when you delete the model using delete() method this trait will listen to the deleting event and delete the images files automatically

Last updated