Skip to content

Commit

Permalink
First commit in new branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Flobbo authored and Flobbo committed Aug 25, 2017
1 parent 61fe3db commit 26b3600
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ return [

Laravel | Crudable
:---------|:----------
5.4 | 0.4
5.3 | 0.4
5.4 | >0.4/2.0
5.3 | >0.4/2.0

**Notice**: If you're planning on using automated binding in Laravel <5.3 you
need to update the config file to reflect the correct usage. Please refer to
Expand Down
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Version History


### v. 2.0

- new and improved code base
- added new features like where statements
- ordering implemented

### v. 1.2

- added orderBy method to include in your query
Expand Down
22 changes: 13 additions & 9 deletions src/Crudable/Contracts/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ public function setRelation(array $relation);
public function orderBy($field, $order = 'asc');

/**
* Create new entry with optional related data
* Create new entry
* @param array $data
* @param string $relationName
*/
public function create(array $data, $relationName = null);
public function create(array $data);

/**
* Update model. Make sure fillable is set on the model
Expand All @@ -81,26 +80,31 @@ public function update($id,array $data,$return_model = false);
public function delete($id, $hardDelete = false);

/**
* Set hasMany relationship by adding the related model and data
* Set hasMany relationship by adding the related model, data and
* relation name
* @param array $data
* @param type $relatedModel
* @param string $relatedModel
* @param string $relation
*/
public function withHasMany(array $data, $relatedModel);
public function withHasMany(array $data, $relatedModel, $relation);

/**
* Add the belongsToMany relationship data to be synced on create/edit
* Add the belongsToMany relationship data to be synced and define
* the relationship name
* @param array $data
* @param string $relation
*/
public function withBelongsToMany(array $data);
public function withBelongsToMany(array $data, $relation);

/**
* Handle a file or photo upload
* @param \Illuminate\Http\Request $request
* @param string $field_name upload field name
* @param string $folder storage folder
* @param string $storage_disk storage disk to be used
* @param bool $randomize to randomize the filename
* @return string filename
*/
public function handleUpload(\Illuminate\Http\Request $request, $field_name = 'photo', $folder = 'images', $storage_disk = 'public');
public function handleUpload(\Illuminate\Http\Request $request, $field_name = 'photo', $folder = 'images', $storage_disk = 'public', $randomize = true);

}
78 changes: 49 additions & 29 deletions src/Crudable/Crudable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

trait Crudable {

protected $relation = [];
protected $withHasMany,$withBelongsToMany,$orderBy,$orderDirection;
protected $relation = [];
protected $withHasMany,$withBelongsToMany,$model;

/**
* Retrieve the Eloquent model
* @return Eloquent model
*/
public function raw(){
return $this->model;
}

/**
* Get a single item or collection
* @param int $id
Expand All @@ -15,22 +24,27 @@ public function get($id = null){
if(!is_null($id)){
return $this->find($id);
}
if(!is_null($this->orderBy)){
return $this->model->with($this->relation)->orderBy($this->orderBy,$this->orderDirection)->get();
}
return $this->model->with($this->relation)->get();
return $this->model->get();
}

/**
* Adds a chainable where statement
* @param string $column
* @param string $operator
* @param mixed $value
* @return self
*/
public function where($column, $operator = null, $value = null){
$this->model = $this->model->where(...func_get_args());
return $this;
}
/**
* Get paginated collection
* @param int $perPage
* @return Collection
*/
public function paginate($perPage){
if(!is_null($this->orderBy)){
return $this->model->with($this->relation)->orderBy($this->orderBy)->paginate($perPage);
}
return $this->model->with($this->relation)->paginate($perPage);
return $this->model->paginate($perPage);
}

/**
Expand All @@ -39,7 +53,7 @@ public function paginate($perPage){
* @return Model
*/
public function find($id){
return $this->model->with($this->relation)->find($id);
return $this->model->find($id);
}

/**
Expand All @@ -51,7 +65,7 @@ public function getTrash($id = null){
if(!is_null($id)){
return $this->getTrashedItem($id);
}
return $this->model->onlyTrashed()->with($this->relation)->get();
return $this->model->onlyTrashed()->get();
}

/**
Expand All @@ -60,7 +74,7 @@ public function getTrash($id = null){
* @return Model
*/
public function getTrashedItem($id){
return $this->model->withTrashed()->with($this->relation)->find($id);
return $this->model->withTrashed()->find($id);
}

/**
Expand All @@ -69,7 +83,7 @@ public function getTrashedItem($id){
* @return self
*/
public function setRelation(array $relation){
$this->relation = $relation;
$this->model = $this->model->with($relation);
return $this;
}

Expand All @@ -79,26 +93,24 @@ public function setRelation(array $relation){
* @param string $order default asc
*/
public function orderBy($field, $order = 'asc'){
$this->orderBy = $field;
$this->orderDirection = $order;
$this->model = $this->model->orderBy(...func_get_args());
return $this;
}

/**
* Create new database entry including related models
* @param array $data
* @param string $relationName
* @return Model
*/
public function create(array $data, $relationName = null){
public function create(array $data){
$model = $this->model->create($data);
//check for hasMany
if(!is_null($this->withHasMany) && !is_null($relationName)){
$model->{$relationName}()->saveMany($this->withHasMany);
if(!is_null($this->withHasMany)){
$model->{$this->withHasMany['relation']}()->saveMany($this->withHasMany['data']);
}
//check for belongsToMany
if(!is_null($this->withBelongsToMany) && !is_null($relationName)){
$model->{$relationName}()->sync($this->withBelongsToMany);
if(!is_null($this->withBelongsToMany)){
$model->{$this->withBelongsToMany['relation']}()->sync($this->withBelongsToMany['data']);
}
return $model;
}
Expand Down Expand Up @@ -138,10 +150,10 @@ public function delete($id, $hardDelete = false){
* @param string $relatedModel
* @return self
*/
public function withHasMany(array $data, $relatedModel){
$this->withHasMany = [];
public function withHasMany(array $data, $relatedModel, $relation_name){
$this->withHasMany['relation'] = $relation_name;
foreach($data as $k=>$v){
$this->withHasMany[] = new $relatedModel($v);
$this->withHasMany['data'][] = new $relatedModel($v);
}
return $this;
}
Expand All @@ -151,8 +163,11 @@ public function withHasMany(array $data, $relatedModel){
* @param array $data
* @return self
*/
public function withBelongsToMany(array $data){
$this->withBelongsToMany = $data;
public function withBelongsToMany(array $data, $relation){
$this->withBelongsToMany = [
'data' => $data,
'relation' => $relation
];
return $this;
}

Expand All @@ -164,13 +179,18 @@ public function withBelongsToMany(array $data){
* @param type $storage_disk
* @return string filename
*/
public function handleUpload(\Illuminate\Http\Request $request, $fieldname = 'photo', $folder = 'images', $storage_disk = 'public'){
public function handleUpload(\Illuminate\Http\Request $request, $fieldname = 'photo', $folder = 'images', $storage_disk = 'public', $randomize = true){
if(!$request->file($fieldname)->isValid()){
throw new \Exception(trans('crud.invalid_file_upload'));
}
//Get filename
$basename = basename($request->file($fieldname)->getClientOriginalName(),'.'.$request->file($fieldname)->getClientOriginalExtension());
$filename = str_slug($basename).'.'.$request->file($fieldname)->getClientOriginalExtension();
if($randomize){
$filename = uniqid().'_'.str_slug($basename).'.'.$request->file($fieldname)->getClientOriginalExtension();
}
else{
$filename = str_slug($basename).'.'.$request->file($fieldname)->getClientOriginalExtension();
}
//Move file to location
$request->file($fieldname)->storeAs($folder,$filename,$storage_disk);
return $filename;
Expand Down
8 changes: 4 additions & 4 deletions src/Crudable/CrudableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public function register(){
$config = $this->app->make('config');
//Check for auto binding
if($config->get('crudable.use_auto_binding')){
foreach($config->get('crudable.implementations') as $imp){
$this->app->when($imp['when'])
->needs(isset($imp['needs'])?$imp['needs']:\Flobbos\Crudable\Contracts\Crud::class)
->give($imp['give']);
foreach($config->get('crudable.implementations') as $usage){
$this->app->when($usage['when'])
->needs(isset($usage['needs'])?$usage['needs']:\Flobbos\Crudable\Contracts\Crud::class)
->give($usage['give']);
}
}
}
Expand Down

0 comments on commit 26b3600

Please sign in to comment.