tomatophp / laravel-tomato
Basic Laravel Helpers for requests and controllers build by TomatoPHP
Fund package maintenance!
3x1io
Requires
- php: ^8.1|^8.2
- spatie/macroable: ^2.0
This package is auto-updated.
Last update: 2024-05-10 15:38:26 UTC
README
Laravel Tomato
Basic Laravel Helpers for requests and controllers build by TomatoPHP
Installation
composer require tomatophp/laravel-tomato
Features
-
Tomato::menu()
inject and handle menus across your application -
Tomato::widget()
inject and handle widgets across your application -
Tomato::slot()
inject and handle slots across your application -
Tomato::request()
handle and validate requests -
Tomato::response()
handle api responses
Usage
you can use the helper by Facade class or by using the helper function
use TomatoPHP\LaravelTomato\Facades\Tomato::register();
or just
tomato()::register();
Register Components
you can register your menu, widget, or slot in your service provider boot()
method
Tomato::register([ Menu::make() ->group(__('CRM')) ->label(__('Name')) ->route('home') ->icon('home'), Widget::make() ->group(__('CRM')) ->label(__('Name')) ->counter(100) ->icon("bx bx-user"), Slot::make() ->position(Positions::dashboardTop) ->view('dashboard-top'), ]);
you can register any type of component and we will check it and handle it for you
all components have a macroable methods to add more functionality to it.
Get Components
you can get your components like this
Tomato::menu()->get(); Tomato::widget()->get(); Tomato::slot()->get();
it will return an array of registered components for you
Request
you can use the request helper to handle and validate your requests like this
return Tomato::request()->index( request: request(), model: App\Models\User::class, );
🔁 Index Request
this method returns view or JsonResponse based on the request type. and we get the request type by check if the route has auth:sanctum
middleware or not.
this method accept some arguments:
request
the request objectmodel
the model you want to getview
the view you want to returntable
the table class you want to usedata
the data you want to pass to the viewapi
if you want to return JsonResponse or notresource
resource class to resource your returned dataquery
if you want to add some query to the modelfilters
if you want to add some filters to the table
public function index(Request $request): View|JsonResponse { return Tomato::index( request: $request, //Required model: $this->model, //Required view: 'users.index', table: \App\Tables\UserTable::class, data: [ 'name' => 'john doe', ], api: true, resource: UserResource::class, query: User::query()->where('is_activated',true), filters: [ 'is_activated', ], ); }
🔁 JSON Request
this method return only json response of the model to make it easy to access it with x-splade-select
or x-tomato-admin-select
this method accept some arguments:
request
the request objectmodel
the model you want to getdata
the data you want to pass to the viewpaginate
if you want to paginate the response or notquery
if you want to add some query to the modelfilters
if you want to add some filters to the table
public function api(Request $request): JsonResponse { return Tomato::json( request: $request, //Required model: \App\Models\User::class, //Required data: [ 'name' => 'john doe', ], paginate: 10, query: User::query()->where('is_activated',true), filters: [ 'is_activated', ], ); }
🔁 Get Request
this method returns view or JsonResponse based on the request type. and we get the request type by check if the route has auth:sanctum
middleware or not.
this method accept some arguments:
model
the model you want to getview
the view you want to returndata
the data you want to pass to the viewhasMedia
if you want to get the media of the model or notcollection [array]
the media collection you want to get as array take true if it's multi or false if it's singleattach [array]
to attach some data to the modelapi
if you want to return JsonResponse or notresource
resource class to resource your returned dataquery
if you want to add some query to the model
public function show(\App\Models\User $model): View|JsonResponse { return Tomato::get( model: $model, //Required view: 'users.show', //Required data: [ 'name' => 'john doe', ], hasMedia: true, collection: [ 'avatar' => false, 'gallery' => true ], attach: [ 'roles' => $model->roles, ], api: true, resource: UserResource::class, query: User::query()->where('is_activated',true) ); }
🔁 Store Request
this method returns RedirectResponse or JsonResponse based on the request type. and we get the request type by check if the route has auth:sanctum
middleware or not.
this method accept some arguments:
request
the request objectmodel
the model you want to getvalidation
the validation rules you want to usemessage
the message you want to return with the responsevalidationError
the message you want to return if the validation failedredirect
the redirect route you want to redirect tohasMedia
if you want to get the media of the model or notcollection [array]
the media collection you want to get as array take true if it's multi or false if it's singleapi
if you want to return JsonResponse or not
public function store(Request $request): RedirectResponse|JsonResponse { $response = Tomato::store( request: $request, //Required model: \App\Models\User::class, //Required validation: [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', ], message: __('User created successfully'), validationError: __('Error When Try To Store User'), redirect: 'admin.users.index', hasMedia: true, collection: [ 'avatar' => false, 'gallery' => true ], api: true, ); if($response instanceof JsonResponse){ return $response; } return $response->redirect; }
🔁 Update Request
this method returns RedirectResponse or JsonResponse based on the request type. and we get the request type by check if the route has auth:sanctum
middleware or not.
this method accept some arguments:
request
the request objectmodel
the model you want to getvalidation
the validation rules you want to usemessage
the message you want to return with the responsevalidationError
the message you want to return if the validation failedredirect
the redirect route you want to redirect tohasMedia
if you want to get the media of the model or notcollection [array]
the media collection you want to get as array take true if it's multi or false if it's singleapi
if you want to return JsonResponse or not
public function update(Request $request, \App\Models\User $model): RedirectResponse|JsonResponse { $response = Tomato::update( request: $request, //Required model: $model, //Required validation: [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', ], message: __('User updated successfully'), redirect: 'admin.users.index', hasMedia: true, collection: [ 'avatar' => false, 'gallery' => true ], api: true, ); if($response instanceof JsonResponse){ return $response; } return $response->redirect; }
🔁 Destroy Request
this method returns RedirectResponse or JsonResponse based on the request type. and we get the request type by check if the route has auth:sanctum
middleware or not.
this method accept some arguments:
model
the model you want to getmessage
the message you want to return with the responseredirect
the redirect route you want to redirect toapi
if you want to return JsonResponse or not
public function destroy(\App\Models\User $model): RedirectResponse|JsonResponse { $response = Tomato::destroy( model: $model, //Required message: __('User deleted successfully'), //Required redirect: 'admin.users.index', ); if($response instanceof JsonResponse){ return $response; } return $response->redirect; }
Request With Media
to make media handling work you must install spatie/laravel-medialibrary
package and run the migration
composer require spatie/laravel-medialibrary php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations" php artisan migrate
and your model must use HasMedia
trait
use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; class User extends Model implements HasMedia { use InteractsWithMedia; }
Handel Alerts
and we have handel Toaster for you if you are using Splade it will working automatically and if you have yoeunes/toastr
package it will working fine too. or you can use fetch toaster
variable from session to get the flash messages.
Support
you can join our discord server to get support TomatoPHP
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security
Please see SECURITY for more information about security.
Credits
License
The MIT License (MIT). Please see License File for more information.