Skip to content

Commit

Permalink
Update model.stub
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Italy authored Nov 1, 2020
1 parent 076cb1d commit 814a458
Showing 1 changed file with 78 additions and 7 deletions.
85 changes: 78 additions & 7 deletions resources/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}

0 comments on commit 814a458

Please sign in to comment.