Add thumbs views in datatables with traits
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Datatables\Botanic;
|
||||
use Yajra\DataTables\Html\Column;
|
||||
use App\Datatables\ParentDataTable as DataTable;
|
||||
use App\Models\Botanic\Specie;
|
||||
use App\Repositories\Botanic\Species;
|
||||
|
||||
class SpeciesDataTable extends DataTable
|
||||
{
|
||||
@@ -12,25 +13,28 @@ class SpeciesDataTable extends DataTable
|
||||
|
||||
public function query(Specie $model)
|
||||
{
|
||||
$model = $model::withCount('varieties')->with('genre');
|
||||
$model = $model::withCount('varieties')->with(['genre','image']);
|
||||
return $this->buildQuery($model);
|
||||
}
|
||||
|
||||
public function modifier($datatables)
|
||||
{
|
||||
$datatables
|
||||
->editColumn('thumb', function (Specie $specie) {
|
||||
return Species::getThumb($specie->image);
|
||||
})
|
||||
->editColumn('genre_name', function (Specie $specie) {
|
||||
return $specie->genre ? $specie->genre->name : '';
|
||||
})
|
||||
->rawColumns(['genre_name', 'action']);
|
||||
->rawColumns(['thumb', 'genre_name', 'action']);
|
||||
return parent::modifier($datatables);
|
||||
}
|
||||
|
||||
|
||||
protected function getColumns()
|
||||
{
|
||||
return [
|
||||
Column::make('name')->title('Nom'),
|
||||
Column::make('thumb')->searchable(false)->orderable(false)->width(40)->class('text-center'),
|
||||
Column::make('alias'),
|
||||
Column::make('genre.name')->data('genre_name')->title('Genre'),
|
||||
Column::make('latin'),
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Datatables\Botanic;
|
||||
use Yajra\DataTables\Html\Column;
|
||||
use App\Datatables\ParentDataTable as DataTable;
|
||||
use App\Models\Botanic\Variety;
|
||||
use App\Repositories\Botanic\Varieties;
|
||||
|
||||
class VarietiesDataTable extends DataTable
|
||||
{
|
||||
@@ -13,7 +14,7 @@ class VarietiesDataTable extends DataTable
|
||||
public function query(Variety $model)
|
||||
{
|
||||
// $model = $model::with('specie')->withCount('Articles')->select('botanic_varieties.*');
|
||||
$model = $model::joinRelationship('Specie')->select('botanic_varieties.*', 'botanic_species.name as specie_name')->with('Specie')->withCount('Articles');
|
||||
$model = $model::joinRelationship('Specie')->select('botanic_varieties.*', 'botanic_species.name as specie_name')->with(['image','Specie'])->withCount('Articles');
|
||||
return $this->buildQuery($model);
|
||||
}
|
||||
|
||||
@@ -21,13 +22,10 @@ class VarietiesDataTable extends DataTable
|
||||
public function modifier($datatables)
|
||||
{
|
||||
$datatables
|
||||
->editColumn('photo', function (Variety $variety) {
|
||||
$media = $variety->getFirstMedia();
|
||||
// dump($media);
|
||||
// return $media('thumb');
|
||||
return '';
|
||||
->editColumn('thumb', function (Variety $variety) {
|
||||
return Varieties::getThumb($variety->image);
|
||||
})
|
||||
->rawColumns(['photo', 'action']);
|
||||
->rawColumns(['thumb', 'action']);
|
||||
return parent::modifier($datatables);
|
||||
}
|
||||
|
||||
@@ -36,9 +34,9 @@ class VarietiesDataTable extends DataTable
|
||||
{
|
||||
return [
|
||||
Column::make('Specie.name')->data('specie_name')->title('Espèce'),
|
||||
Column::make('thumb')->searchable(false)->orderable(false)->width(40)->class('text-center'),
|
||||
Column::make('name')->title('Nom'),
|
||||
Column::make('articles_count')->title('Nb articles')->class('text-right')->searchable(false),
|
||||
Column::make('photo')->title('')->searchable(false)->orderable(false),
|
||||
$this->makeColumnButtons(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -7,10 +7,13 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
use Wildside\Userstamps\Userstamps;
|
||||
use Rinvex\Tags\Traits\Taggable;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
|
||||
class Specie extends Model
|
||||
use App\Traits\Model\Imageable;
|
||||
|
||||
class Specie extends Model implements HasMedia
|
||||
{
|
||||
use SoftDeletes, Taggable, UserStamps;
|
||||
use Imageable, SoftDeletes, Taggable, UserStamps;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
protected $table = 'botanic_species';
|
||||
@@ -22,12 +25,12 @@ class Specie extends Model
|
||||
|
||||
public function Genre()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Botanic\Genre');
|
||||
return $this->belongsTo(Genre::class);
|
||||
}
|
||||
|
||||
public function Varieties()
|
||||
{
|
||||
return $this->hasMany('App\Models\Botanic\Variety');
|
||||
return $this->hasMany(Variety::class);
|
||||
}
|
||||
|
||||
public function Articles()
|
||||
@@ -37,6 +40,6 @@ class Specie extends Model
|
||||
|
||||
public function scopeByName($query, $name)
|
||||
{
|
||||
return $query->where('name', $name);
|
||||
return $query->where($this->table . '.name', $name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,19 +5,16 @@ namespace App\Models\Botanic;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
use Spatie\Image\Manipulations;
|
||||
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
use Rinvex\Tags\Traits\Taggable;
|
||||
use Kirschbaum\PowerJoins\PowerJoins;
|
||||
use Wildside\Userstamps\Userstamps;
|
||||
|
||||
use App\Traits\Model\Imageable;
|
||||
|
||||
class Variety extends Model implements HasMedia
|
||||
{
|
||||
use InteractsWithMedia, PowerJoins, SoftDeletes, Taggable, UserStamps;
|
||||
use Imageable, PowerJoins, SoftDeletes, Taggable, UserStamps;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
protected $table = 'botanic_varieties';
|
||||
@@ -36,13 +33,4 @@ class Variety extends Model implements HasMedia
|
||||
{
|
||||
return $this->morphToMany('App\Models\Shop\Tag', 'taggable');
|
||||
}
|
||||
|
||||
public function registerMediaConversions(Media $media = null) : void
|
||||
{
|
||||
$this->addMediaConversion('thumb')->fit(Manipulations::FIT_CROP, 32, 32);
|
||||
$this->addMediaConversion('mini')->fit(Manipulations::FIT_CROP, 96, 96);
|
||||
$this->addMediaConversion('preview')->fit(Manipulations::FIT_CROP, 160, 160);
|
||||
$this->addMediaConversion('normal')->fit(Manipulations::FIT_CROP, 480, 480);
|
||||
// $this->addMediaConversion('zoom')->fit(Manipulations::FIT_CROP, 1200, 1200)->withResponsiveImages();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace App\Models\Core;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\Model\HasComments;
|
||||
|
||||
class Comment extends Model
|
||||
{
|
||||
|
||||
@@ -16,7 +16,7 @@ use Kirschbaum\PowerJoins\PowerJoins;
|
||||
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
|
||||
use Wildside\Userstamps\Userstamps;
|
||||
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\Model\HasComments;
|
||||
|
||||
class Article extends Model implements HasMedia
|
||||
{
|
||||
@@ -70,11 +70,9 @@ class Article extends Model implements HasMedia
|
||||
return $query->where($this->table . '.id', $id);
|
||||
}
|
||||
|
||||
public function scopeByCategory($query, $category_id)
|
||||
public function scopeByAutocomplete($query, $str)
|
||||
{
|
||||
return $query->whereHas('categories', function ($query) use ($category_id) {
|
||||
$query->where('id', $category_id);
|
||||
});
|
||||
return $query->where($this->table . '.name', 'LIKE', "%${str}%");
|
||||
}
|
||||
|
||||
public function scopeByArticleNature($query, $id)
|
||||
@@ -82,6 +80,13 @@ class Article extends Model implements HasMedia
|
||||
return $query->where($this->table . '.article_nature_id', $id);
|
||||
}
|
||||
|
||||
public function scopeByCategory($query, $category_id)
|
||||
{
|
||||
return $query->whereHas('categories', function ($query) use ($category_id) {
|
||||
$query->where('id', $category_id);
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeByProduct($query, $model)
|
||||
{
|
||||
return $query->where($this->table . '.product_type', $model);
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace App\Models\Shop;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\Model\HasComments;
|
||||
|
||||
class Offer extends Model
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use Znck\Eloquent\Traits\BelongsToThrough;
|
||||
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\Model\HasComments;
|
||||
|
||||
class PriceList extends Model
|
||||
{
|
||||
|
||||
@@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
|
||||
use Kirschbaum\PowerJoins\PowerJoins;
|
||||
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\Model\HasComments;
|
||||
|
||||
class Tariff extends Model
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace App\Models\Shop;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\Model\HasComments;
|
||||
|
||||
class Variation extends Model
|
||||
{
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace App\Repositories\Botanic;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
@@ -11,9 +9,11 @@ use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Repositories\Core\Tag;
|
||||
use App\Models\Botanic\Specie;
|
||||
use App\Exports\Botanic\SpeciesExport;
|
||||
use App\Traits\Repository\Imageable;
|
||||
|
||||
class Species
|
||||
{
|
||||
use Imageable;
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
@@ -96,21 +96,6 @@ class Species
|
||||
return Tag::storeTags($specie, $tags);
|
||||
}
|
||||
|
||||
public static function storeImages($specie, $files)
|
||||
{
|
||||
return Media::storeImages($specie, $files);
|
||||
}
|
||||
|
||||
public static function getImages($id)
|
||||
{
|
||||
return Media::getImages(self::get($id));
|
||||
}
|
||||
|
||||
public static function deleteImage($id, $index)
|
||||
{
|
||||
return Media::deleteImage(self::get($id), $index);
|
||||
}
|
||||
|
||||
public static function exportExcel()
|
||||
{
|
||||
return Excel::download(new SpeciesExport, 'species.xlsx');
|
||||
|
||||
@@ -7,12 +7,14 @@ use Illuminate\Support\Str;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
|
||||
use App\Repositories\Core\Tag;
|
||||
use App\Repositories\Core\Media;
|
||||
use App\Models\Botanic\Variety;
|
||||
use App\Exports\Botanic\VarietiesExport;
|
||||
use App\Traits\Repository\Imageable;
|
||||
|
||||
class Varieties
|
||||
{
|
||||
use Imageable;
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
return Variety::orderBy('name')->get()->pluck('name', 'id')->toArray();
|
||||
@@ -102,21 +104,6 @@ class Varieties
|
||||
return Tag::storeTags($variety, $tags);
|
||||
}
|
||||
|
||||
public static function storeImages($variety, $files)
|
||||
{
|
||||
return Media::storeImages($variety, $files);
|
||||
}
|
||||
|
||||
public static function getImages($id)
|
||||
{
|
||||
return Media::getImages(self::get($id));
|
||||
}
|
||||
|
||||
public static function deleteImage($id, $index)
|
||||
{
|
||||
return Media::deleteImage(self::get($id), $index);
|
||||
}
|
||||
|
||||
public static function exportExcel()
|
||||
{
|
||||
return Excel::download(new VarietiesExport, 'varieties.xlsx');
|
||||
|
||||
@@ -11,11 +11,14 @@ use App\Repositories\Botanic\Species;
|
||||
use App\Repositories\Botanic\Varieties;
|
||||
use App\Models\Shop\Article;
|
||||
|
||||
use App\Traits\Repository\Imageable;
|
||||
class Articles
|
||||
{
|
||||
use Imageable;
|
||||
|
||||
public static function autocomplete($str)
|
||||
{
|
||||
$data = Article::where('name', 'LIKE', "%${str}%")->orderBy('name')->limit(30)->get()->pluck('name', 'id');
|
||||
$data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->get()->pluck('name', 'id');
|
||||
$export = [];
|
||||
foreach ($data as $key => $name) {
|
||||
$export[] = ['value' => $key, 'text' => $name];
|
||||
@@ -269,33 +272,4 @@ class Articles
|
||||
return Tag::storeTags($article, $tags);
|
||||
}
|
||||
|
||||
public static function storeImages($article, $files)
|
||||
{
|
||||
return Medias::storeImages($article, $files);
|
||||
}
|
||||
|
||||
public static function storeImage($article, $file)
|
||||
{
|
||||
return Medias::storeImage($article, $file);
|
||||
}
|
||||
|
||||
public static function getImages($id)
|
||||
{
|
||||
return Medias::getImages(self::get($id));
|
||||
}
|
||||
|
||||
public static function getThumbSrc($image)
|
||||
{
|
||||
return Medias::getThumbSrc($image);
|
||||
}
|
||||
|
||||
public static function getPreviewSrc($image)
|
||||
{
|
||||
return Medias::getPreviewSrc($image);
|
||||
}
|
||||
|
||||
public static function deleteImage($id, $index)
|
||||
{
|
||||
return Medias::deleteImage(self::get($id), $index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
namespace App\Traits\Model;
|
||||
|
||||
trait CanComment
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
namespace App\Traits\Model;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Contracts\Commentator;
|
||||
34
app/Traits/Model/Imageable.php
Normal file
34
app/Traits/Model/Imageable.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Model;
|
||||
|
||||
use Spatie\Image\Manipulations;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
|
||||
use App\Repositories\Core\Medias;
|
||||
|
||||
trait Imageable
|
||||
{
|
||||
use InteractsWithMedia;
|
||||
|
||||
public function images()
|
||||
{
|
||||
return $this->hasMany('App\Models\Core\Media', 'model_id')->where('model_type', get_class($this));
|
||||
}
|
||||
|
||||
public function image()
|
||||
{
|
||||
return $this->hasOne('App\Models\Core\Media', 'model_id')->where('model_type', get_class($this));
|
||||
}
|
||||
|
||||
public function registerMediaConversions(Media $media = null) : void
|
||||
{
|
||||
$this->addMediaConversion('thumb')->fit(Manipulations::FIT_CROP, 32, 32);
|
||||
$this->addMediaConversion('mini')->fit(Manipulations::FIT_CROP, 96, 96);
|
||||
$this->addMediaConversion('preview')->fit(Manipulations::FIT_CROP, 160, 160);
|
||||
$this->addMediaConversion('normal')->fit(Manipulations::FIT_CROP, 480, 480);
|
||||
// $this->addMediaConversion('zoom')->fit(Manipulations::FIT_CROP, 1200, 1200)->withResponsiveImages();
|
||||
}
|
||||
|
||||
}
|
||||
43
app/Traits/Repository/Imageable.php
Normal file
43
app/Traits/Repository/Imageable.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Repository;
|
||||
|
||||
use App\Repositories\Core\Medias;
|
||||
|
||||
trait Imageable
|
||||
{
|
||||
public static function storeImages($variety, $files)
|
||||
{
|
||||
return Medias::storeImages($variety, $files);
|
||||
}
|
||||
|
||||
public static function getImages($id)
|
||||
{
|
||||
return Medias::getImages(self::get($id));
|
||||
}
|
||||
|
||||
public static function getThumb($image)
|
||||
{
|
||||
return '<img src="' . self::getThumbSrc($image) . '">';
|
||||
}
|
||||
|
||||
public static function getThumbSrc($image)
|
||||
{
|
||||
return Medias::getThumbSrc($image);
|
||||
}
|
||||
|
||||
public static function getPreview($image)
|
||||
{
|
||||
return '<img src="' . self::getPreviewSrc($image) . '">';
|
||||
}
|
||||
|
||||
public static function getPreviewSrc($image)
|
||||
{
|
||||
return Medias::getPreviewSrc($image);
|
||||
}
|
||||
|
||||
public static function deleteImage($id, $index)
|
||||
{
|
||||
return Medias::deleteImage(self::get($id), $index);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user