namespace App\Exceptions;
use Exception;
class InvalidOrderException extends Exception
{
// ...
/**
* Get the exception's context information.
*
* @return array
*/
public function context()
{
return ['order_id' => $this->orderId];
}
}
public function store(CategoryRequest $request, CreateCategory $action)
{
try {
$action->execute($request->validated());
return response()->json([
'message' => Lang::get('database.store_success')
]);
} catch (Exception $e) {
return $this->errorStringApiResponse($e->getMessage(), 500);
}
}
class CreateCategory
{
public function execute(array $data)
{
$store = new Category();
$store->name = $data['name'];
$store->icon = $data['icon'];
$store->description = $data['description'];
$store->text = $data['text'];
$store->meta_title = $data['meta_title'];
$store->meta_description = $data['meta_description'];
$store->is_published = $data['is_published'];
$store->slug = $data['slug'];
$store->save();
}
}
$store = new Category();
$store->name = $data['name'];
$store->icon = $data['icon'];
$store->description = $data['description'];
$store->text = $data['text'];
$store->meta_title = $data['metaTitle'] ?? $data['name'];
$store->meta_description = $data['metaDescription'] ?? $data['metaDescription'];
$store->is_published = $data['isPublished'];
$store->slug = $data['slug'] ?? Str::slug($data['title'], '-');
$store->save();
$data = $request->only([])
Category::create($data);
class CategoryController extends Controller
{
use JsonResponseTrait;
public function __construct(
private CategoryService $categoryService
)
{
}
/**
* Display a listing of the resource.
*
* @return JsonResponse
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return JsonResponse
*/
public function store(CategoryRequest $request)
{
try {
$this->categoryService->store($request->validated());
return $this->successApiResponse([], Lang::get('database.store_success'));
} catch (Exception $e) {
return $this->errorStringApiResponse($e->getMessage(), 500);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return JsonResponse
*/
public function show(int $id)
{
$category = Category::find($id);
if (!$category) return $this->notFoundApiResponse([], '404');
return new CategoryResource($category);
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return JsonResponse
*/
public function update(CategoryRequest $request, int $id)
{
$category = Category::find($id);
if (!$category) return $this->notFoundApiResponse([], Lang::get('database.record_not_fount'));
try {
$this->categoryService->update($category, $request->validated());
return $this->successApiResponse([], Lang::get('database.update_success'));
} catch (Exception $e) {
return $this->errorStringApiResponse($e->getMessage(), 500);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return JsonResponse
*/
public function destroy(int $id)
{
$category = Category::find($id);
if (!$category) return $this->notFoundApiResponse([], Lang::get('database.record_not_fount'));
try {
$category->delete();
return $this->successApiResponse([], Lang::get('database.delete_success'));
} catch (Exception $e) {
return $this->errorStringApiResponse($e->getMessage(), 500);
}
}
}
class CategoryService
{
public function store(array $data): void
{
$store = new Category();
$store->name = trim($data['name']);
$store->icon = trim($data['icon']);
$store->description = trim($data['description']);
$store->text = $data['text'];
$store->meta_title = $data['metaTitle'] ?? $data['name'];
$store->meta_description = $data['metaDescription'] ?? $data['metaDescription'];
$store->is_published = $data['isPublished'];
$store->slug = trim($data['slug']) ?? Str::slug($data['title'], '-');
$store->save();
}
public function update(Model $category, array $data): void
{
$category->name = trim($data['name']);
$category->icon = trim($data['icon']);
$category->description = trim($data['description']);
$category->text = $data['text'];
$category->meta_title = $data['metaTitle'] ?? $data['name'];
$category->meta_description = $data['metaDescription'] ?? $data['metaDescription'];
$category->is_published = $data['isPublished'];
$category->slug = trim($data['slug']) ?? Str::slug($data['title'], '-');
$category->save();
}
}
я в мета храню тип меню
meta: {layout: '', menu: 'crm'}