diff --git a/resources/stubs/model.stub b/resources/stubs/model.stub index 409c289..84842a1 100755 --- a/resources/stubs/model.stub +++ b/resources/stubs/model.stub @@ -2,21 +2,92 @@ namespace DummyNamespace; +use DB; +//use App\Models\Image; +use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model; -//use Illuminate\Database\Eloquent\SoftDeletes; class DummyClass extends Model { - // use SoftDeletes; protected $guarded = []; - protected $fillable = [ + // imageable polymorphic + // public function image() { + // return $this->morphOne(Image::class, 'imageable'); + // } - ]; + // fetch Data + public static function fetchData($value='') + { + // this way will fire up speed of the query + $obj = self::query(); - protected $dates = [ - 'created_at', 'updated_at', - ]; + // langauges in case you use multilanguages transactions package.. + if(isset($value['locale']) && $value['locale']) { + app()->setLocale($value['locale']); + } + // search for multiple columns.. + if(isset($value['search']) && $value['search']) { + $obj->where(function($q) use ($value){ + $q->where('title', 'like','%'.$value['search'].'%'); + $q->orWhere('id', $value['search']); + }); + } + + // order By.. + if(isset($value['sort']) && $value['sort']) { + $obj->orderBy('id', $value['sort']); + } else { + $obj->orderBy('id', 'DESC'); + } + + + // feel free to add any query filter as much as you want... + + + + $obj = $obj->paginate($value['paginate'] ?? 10); + return $obj; + } + + // Create or Update + public static function createOrUpdate($id, $value) + { + try { + + // begin Transaction between tables + DB::beginTransaction(); + + // find Or New + $row = (isset($id)) ? self::find($id) : new self; + $row->title = $value['title'] ?? NULL; + $row->body = $value['body'] ?? NULL; + $row->save(); + + // Image + // if(isset($value['image'])) { + // $row->image()->delete(); + // if($value['image']) { + // if(!Str::contains($value['image'], [ Imageable::contains() ])) { + // $image = Image::uploadImage($value['image']); + // } else { + // $image = explode('/', $value['image']); + // $image = end($image); + // } + // $row->image()->create([ 'url' => $image ]); + // } + // } + + + DB::commit(); + // End Commit of Transaction + + return true; + } catch (\Exception $e) { + DB::rollback(); + return $e->getMessage(); + } + } }