/** * Run the migrations. * * @returnvoid */publicfunctionup() {Schema::table('categories',function (Blueprint $table) { $table->json('image')->nullable(); }); }
Inside the model class:
<?phpnamespaceApp;useHusseinFeras\Laraimage\Traits\SingleColumnSingleImage;useIlluminate\Database\Eloquent\Model;classCategoryextendsModel{useSingleColumnSingleImage;/* * 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 */publicfunctionimagesPath() {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 */publicfunctionaddImage(string $requestKey) :void {}/* * delete the image * * @return boolean */publicfunctiondeleteImage():bool {}/* * get the image url * * @return string image url | default image */publicfunctiongetImage() {}
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
/** * Run the migrations. * * @returnvoid */publicfunctionup() {Schema::table('products',function (Blueprint $table) { $table->json('images')->nullable(); }); }
Inside the model class:
<?phpnamespaceApp;useHusseinFeras\Laraimage\Traits\SingleColumnMultiImage;useIlluminate\Database\Eloquent\Model;classProductextendsModel{useSingleColumnMultiImage;/* * 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 */publicfunctionimagesPath() {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 */publicfunctionaddImages($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 */publicfunctiondeleteImages($id =null):bool {}/* * get the images urls * * @return array images url | default image */publicfunctiongetImages():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
phpartisanmake:migrationadd_image_to_posts_table
the migration file:
/** * Run the migrations. * * @returnvoid */publicfunctionup() {Schema::table('posts',function (Blueprint $table) { $table->json('poster')->nullable(); $table->json('cover')->nullable(); }); }
Inside the model class:
<?phpnamespaceApp;useHusseinFeras\Laraimage\Traits\MultiColumnSingleImage;useIlluminate\Database\Eloquent\Model;classPostextendsModel{useMultiColumnSingleImage;/* * 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 */publicfunctionimagesPath() {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 */publicfunctionaddImage($imageColumn,$requestKey):void {}/* * delete the image by specifying the column * * @param string $imageColumn * @return boolean */publicfunctiondeleteImage($imageColumn):bool {}/* * delete all images in all image columns * */publicfunctiondeleteAllImages():void {}/* * get the image url by specifying the column * * @param string $imageColumn * @return string image url | default image */publicfunctiongetImage($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
phpartisanmake:migrationadd_images_to_posts_table
the migration file:
/** * Run the migrations. * * @returnvoid */publicfunctionup() {Schema::table('posts',function (Blueprint $table) { $table->json('slider')->nullable(); $table->json('images')->nullable(); }); }
Inside the model class:
<?phpnamespaceApp;useHusseinFeras\Laraimage\Traits\MultiColumnMultiImage;useIlluminate\Database\Eloquent\Model;classPostextendsModel{useMultiColumnMultiImage;/* * 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 */publicfunctionimagesPath() {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 */publicfunctionaddImages($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 */publicfunctiondeleteImages($imagesColumn,$id =null):bool {}/* * delete all images in all image columns * */publicfunctiondeleteAllImages():void {}/* * get the images urls * * @return array images url | default image */publicfunctiongetImages($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