This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
344d24a
commit 1081c32
Showing
48 changed files
with
1,146 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Spatie\EloquentSortable\Sortable; | ||
use Spatie\EloquentSortable\SortableTrait; | ||
use Spatie\Translatable\HasTranslations; | ||
|
||
/** | ||
* App\Category | ||
* | ||
* @property int $id | ||
* @property string $name | ||
* @property string $type | ||
* @property int|null $parent_id | ||
* @property int|null $order_column | ||
* @property \Illuminate\Support\Carbon|null $created_at | ||
* @property \Illuminate\Support\Carbon|null $updated_at | ||
* @property Category $parent | ||
* @method static \Illuminate\Database\Eloquent\Builder|Category newModelQuery() | ||
* @method static \Illuminate\Database\Eloquent\Builder|Category newQuery() | ||
* @method static \Illuminate\Database\Eloquent\Builder|Category query() | ||
* @mixin \Eloquent | ||
*/ | ||
class Category extends Model implements Sortable | ||
{ | ||
use HasTranslations; | ||
use SortableTrait; | ||
|
||
protected $fillable = ['name']; | ||
|
||
public $translatable = ['name']; | ||
|
||
protected function getLocale(): string | ||
{ | ||
return request()->header('locale') ?: app()->getLocale(); | ||
} | ||
|
||
public function parent() | ||
{ | ||
return $this->belongsTo(Category::class); | ||
} | ||
|
||
public function children() | ||
{ | ||
return $this->hasMany(Category::class, 'parent_id'); | ||
} | ||
|
||
public function buildSortQuery() | ||
{ | ||
return static::query() | ||
->where('type', $this->type) | ||
->where('parent_id', $this->parent_id); | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
]; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
examples/demo-laravel/app/Http/Controllers/CategoryController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\StoreCategory; | ||
use App\Http\Requests\UpdateCategory; | ||
use App\Http\Resources\Category as CategoryResource; | ||
use App\Category; | ||
use Illuminate\Http\Request; | ||
use Spatie\QueryBuilder\AllowedFilter; | ||
use Spatie\QueryBuilder\QueryBuilder; | ||
|
||
class CategoryController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->authorizeResource(Category::class); | ||
} | ||
|
||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection | ||
*/ | ||
public function index() | ||
{ | ||
return CategoryResource::collection( | ||
QueryBuilder::for(Category::class) | ||
->allowedFields(['id', 'name']) | ||
->allowedFilters([ | ||
AllowedFilter::exact('id'), | ||
AllowedFilter::exact('type'), | ||
'name', | ||
]) | ||
->ordered() | ||
->get() | ||
); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param \App\Category $category | ||
* @return CategoryResource | ||
*/ | ||
public function show(Category $category) | ||
{ | ||
return new CategoryResource($category); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param Request $request | ||
* @return CategoryResource | ||
*/ | ||
public function store(StoreCategory $request) | ||
{ | ||
$category = Category::create($request->all()); | ||
|
||
return new CategoryResource($category); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param UpdateCategory $request | ||
* @param \App\Category $category | ||
* @return CategoryResource | ||
*/ | ||
public function update(UpdateCategory $request, Category $category) | ||
{ | ||
$category->update($request->all()); | ||
|
||
return new CategoryResource($category); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param \App\Category $category | ||
* @return \Illuminate\Http\Response | ||
* @throws \Exception | ||
*/ | ||
public function destroy(Category $category) | ||
{ | ||
$category->delete(); | ||
|
||
return response()->noContent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class StoreCategory extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => 'required', | ||
'type' => 'required|in:book', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
examples/demo-laravel/app/Http/Requests/UpdateCategory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UpdateCategory extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => 'sometimes|required', | ||
'type' => 'sometimes|required|in:book', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Vtec\Crud\Http\Resources\BaseResource; | ||
|
||
class Category extends BaseResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
Oops, something went wrong.